If in my code I have the following snippet:
try {
doSomething();
} catch (...) {
doSomethingElse();
throw;
}
Will the throw rethrow the specific exception caught by the default ellipsis handler?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes. The exception is active until it’s caught, where it becomes inactive. But it lives until the scope of the handler ends. From the standard, emphasis mine:
That is:
Between those arrows, you can re-throw the exception. Only when the handlers scope ends does the exception cease to exist.
In fact, in §15.1/6 the example given is nearly the same as your code:
Keep in mind if you
throwwithout an active exception,terminatewill be called. This cannot be the case for you, being in a handler.If
doSomethingElse()throws and the exception has no corresponding handler, because the original exception is considered handled the new exception will replace it. (As if it had just thrown, begins stack unwinding, etc.)That is:
Of course if nothing throws,
throw;will be reached and you’ll throw your caught exception as expected.