Boost logo

Boost :

Subject: Re: [boost] Boost and exceptions
From: Kim Barrett (kab.conundrums_at_[hidden])
Date: 2012-06-23 15:30:18


On Jun 23, 2012, at 3:11 PM, Robert Ramey wrote:
> To summarize, I don't see how just changing boost::throw_exception
> without changing anything else can possible add anything useful to
> the library or the users application.
> [...]
> Hmmm - I don't see how just changing the implementation of
> boost::throw_exception would change this in anyway. Perhaps you're
> suggesting that in addition to chaning the functionality of boost::throw
> the serialization libary should redefine archive_exception to derive
> from boost::exception rather directly from std::exception. Of course
> that's a totally different topic than we've been discussing up until now.
> and I guess would be a new thread. But maybe you're not suggesting
> that. Sorry if I got this wrong.

This is incorrect.

boost::throw_exception (in the interesting case, where
BOOST_NO_EXCEPTIONS and BOOST_EXCEPTION_DISABLE are both undefined)
ensures that the thrown object is derived from boost::exception even
if the argument is not. It does this (via enable_error_info(), which
does the heavy lifting) by examining the type E of the argument. If E
is derived from boost::exception, then just use the argument as is.
Otherwise, construct a new object. The class of the new object
derives from both E and boost::exception (multiple inheritance), and
has a conversion constructor for arguments of type E which passes the
argument to the new class's base E object's copy constructor. Apply
that constructor to the original argument, and use the resulting
object. (Note that throw requires the argument to be copy
constructible, so the described mechanism imposes no new constraint on
E by requiring copy construction.)

None of this *requires* any intervention by calling code or library
authors. There is no requirement to explicitly derive from
boost::exception for this to work. A library developer might choose
to derive an exception class from boost::exception, since that will
simplify the resulting generated code and eliminate a copy
construction in invocations of boost::throw_exception, but one gets a
boost::exception based object regardless of what came in.

[Note that boost::exception is *not* derived from std::exception; it is
a "mixin" class that is typically derived from by multiple
inheritance.]


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk