When I use boost::copy_exception
to copy an exception to an exception_ptr
, I lose type information. Take a look at the following code:
This is by design, and your analysis is correct: the static type is used, not the dynamic type. In fact, to avoid this surprise, boost::copy_exception
became std::make_exception_ptr
during the process that led to C++11. That way, it's clearer that current_exception
(whether the Boost version or the C++11 one) is the correct thing to use to correctly capture, well, the current exception. I highly recommend using BOOST_THROW_EXCEPTION
, or at least boost::throw_exception
, when it comes to using Boost.Exception-enabled exceptions in your code.
When it comes to third-party code, there is no solution other than the one you already mentioned, or some other moral equivalent (like dynamic_cast
-ing to the different leaf classes that make up the exception type hierarchy or hierarchies, or typeid
abuse).
In this respect exception handling is the same as working with one or more hierarchy of polymorphic types, and the operation you're attempting in this case is a virtual copy, also known as cloning: either your code is intrusively designed to support that (as is the case when using e.g. BOOST_THROW_EXCEPTION(e);
instead of throw e;
), or you're going to painfully walk the inheritance tree. Note that you can at least refactor that pain into just one site, such that you'd end up with e.g.
try {
third_party_api();
} catch(...) {
ptr = catch_exceptions_from_api();
}