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:
try {
throw std::runtime_error("something");
} catch (exception& e) {
ptr = boost::copy_exception(e);
}
if (ptr) {
try {
boost::rethrow_exception(ptr);
} catch (std::exception& e) {
cout << e.what() << endl;
cout << boost::diagnostic_information(e) << endl;
}
}
From this, I get the following output:
N5boost16exception_detail10clone_implISt9exceptionEE
Dynamic exception type: boost::exception_detail::clone_impl<std::exception>
std::exception::what: N5boost16exception_detail10clone_implISt9exceptionEE
So basically boost::copy_exception
statically copied the argument it got.
This problem is solved if I throw my exception with boost::enable_current_exception
instead, like this.
try {
throw boost::enable_current_exception(std::runtime_error("something"));
} catch (...) {
ptr = boost::current_exception();
}
if (ptr) {
try {
boost::rethrow_exception(ptr);
} catch (std::exception& e) {
cout << e.what() << endl;
cout << boost::diagnostic_information(e) << endl;
}
}
The problem with this is that sometimes the exceptions are thrown by a library that does not use boost::enable_current_exception
. In this case, is there any way to put the exception into an exception_ptr
apart from catching each kind of possible exception one by one and use boost::copy_exception
on each one?