suppose the following:
struct wrapper_exception {
std::runtime_error& err;
wrapper_exception( std::runtime_error& _e ) : err(e) {}
};
try {
throw std::runtime_error("foo");
} catch (std::runtime_error& err) {
throw wrapper_exception( err);
}
question: Is it safe to access the reference to runtime_error inside wrapper_exception after it is handled?
I’m very tempted to say that this is undefined behavior.
The standard says repeatedly that the lifetime of the exception objects ends when the active handler exits other than by rethrowing the exception, i.e. by saying
throw;.Since you do not rethrow the exception, the original exception object expires at the end of the catch block and you end up with a dangling reference, no different from throwing an object that holds a reference to a local variable.