当我使用boost :: copy_exception将异常复制到exception_ptr时,我丢失了类型信息.看看下面的代码:
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;
}
}
从这里,我得到以下输出:
N5boost16exception_detail10clone_implISt9exceptionEE
Dynamic exception type: boost::exception_detail::clone_impl<:exception>
std::exception::what: N5boost16exception_detail10clone_implISt9exceptionEE
所以基本上boost :: copy_exception静态地复制了它得到的参数.
如果我使用boost :: enable_current_exception抛出异常,就会解决这个问题,就像这样.
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;
}
}
这样做的问题是,有时异常是由不使用boost :: enable_current_exception的库抛出的.在这种情况下,有没有办法将异常放入exception_ptr,除了逐个捕获每种可能的异常并在每一个上使用boost :: copy_exception?