|
Boost : |
Subject: Re: [boost] Boost and exceptions
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2012-06-25 03:23:14
On Sun, Jun 24, 2012 at 11:33 PM, Robert Ramey <ramey_at_[hidden]> wrote:
> Emil Dotchevski wrote:
> > On Sat, Jun 23, 2012 at 4:19 PM, Robert Ramey <ramey_at_[hidden]> wrote:
> > If you can think of a way to provide this functionality less
> > intrusively,
> > I'm all ears.
>
> I've attached as 100 line test program which I believe demonstrates how
> functionality like boost::exception can be implemented.
>
<snipped code>
First, as you point out, your code can't be deployed even if it was
correct, because many compilers don't yet implement std::exception_ptr.
Secondly, your program erases the type of the exception object. In C++, the
type of the exception and not its value corresponds to its semantics.
That's why the language lets you catch an exception and rethrow it without
erasing its type, by using throw without arguments.
The program below demonstrates the functionality to be replicated if you're
willing to give it another try. Note that app_function stores app-specific
info in the exception object without knowing or altering its type.
//Library code:
#include <boost/throw_exception.hpp>
struct lib_exception: public std::exception { };
void
lib_function()
{
boost::throw_exception(lib_exception());
}
//Application code:
#include <boost/exception/info.hpp>
#include <boost/exception/get_error_info.hpp>
typedef boost::error_info<struct app_info_,int> app_info;
void
app_function()
{
try
{
lib_function();
}
catch( boost::exception & e )
{
e << app_info(42);
throw;
}
}
int
main()
{
try
{
app_function();
}
catch( lib_exception & e )
{
std::cout << "Caught a lib_exception with app_info=" <<
*boost::get_error_info<app_info>(e);
}
}
Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk