I really like std::throw_with_nested in c++11 since it emulates java\'s printStackTrace() but right now I am just curious how to catch a nested exception, for example:
Unwrapping a std::nested_exception
is readily accomplished via recursion:
template<typename E>
void rethrow_unwrapped(const E& e)
{
try {
std::rethrow_if_nested(e);
} catch(const std::nested_exception& e) {
rethrow_unwrapped(e);
} catch(...) {
throw;
}
}
Where usage would look something like this:
try {
throws();
} catch(const std::exception& e) {
try {
rethrow_unwrapped(e);
} catch (const SomeException& e) {
// do something
}
}
A demo of this in action can be found here.