Back
In C#, I can use the throw; statement to rethrow an exception while preserving the stack trace:
throw;
try{ ...}catch (Exception e){ if (e is FooException) throw;}
try
{
...
}
catch (Exception e)
if (e is FooException)
Is there something like this in Java (that doesn't lose the original stack trace)?
You can try:
catch (WhateverException e) { throw e;}
catch (WhateverException e) {
throw e;
It will just rethrow the exception you've passed (certainly the enclosing method has to allow this via its signature etc.). The exception will preserve the initial stack trace.
31k questions
32.8k answers
501 comments
693 users