Use throw_with_nested and catch nested exception
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:
void f() {
try {
throw SomeException();
} catch( ... ) {
std::throw_with_nested( std::runtime_error( "Inside f()" ) );
}
}
void g() {
try {
f();
} catch( SomeException & e ) { // I want to catch SomeException here,
not std::runtime_error, :(
// do something
}
}
Previously, I thought std::throw_with_nested generates a new exception
which is multiply derived from two exceptions (std::runtime_error and
SomeException) but after reading some online tutorial, it encapsulates
SomeException within std::exception_ptr and it's probabaly why I canonot
catch it.
Then I realized I can fix this by using std::rethrow_if_nested( e ) but
the above case is only two level which is easy to handle but thinking
about more general situation like 10 level folded and I just don't want to
write std::rethrow_if_nested 10 times to handle it.
Any suggestions would be really appreciated.
No comments:
Post a Comment