When you catch a boost::exception, you can call clone_exception() to get an exception_ptr object:
#include <boost/exception/cloning.hpp> #include <boost/thread.hpp> #include <boost/bind.hpp> void do_work(); //throws cloning-enabled boost::exceptions void worker_thread( boost::exception_ptr & error ) { try { do_work(); error = boost::exception_ptr(); } catch( boost::exception & e ) { error = boost::clone_exception(e); } }
In the above example, note that clone_exception() captures the original type of the exception object, even though e refers to the base type boost::exception. This original type can be thrown again using the rethrow_exception() function:
// ...continued
void
work()
{
boost::exception_ptr error;
boost::thread t( boost::bind(worker_thread,boost::ref(error)) );
t.join();
if( error )
boost::rethrow_exception(error);
}
Clone_exception() could fail to copy the original exception object in the following cases:
Regardless, the use of clone_exception() and rethrow_exception() in the above examples is well-formed.