Reputation: 1165
Why is the stack trace Exception in thread "main" java.lang.ArithmeticException: 3
and not Exception in thread "main" java.lang.ArithmeticException: 1
?
Isn't ArithmeticException("1")
thrown first?
try{
try{
throw new ArithmeticException("1");
}finally{
throw new ArithmeticException("2");
}
}finally{
throw new ArithmeticException("3");
}
Upvotes: 1
Views: 85
Reputation: 140328
Yes, the "1"
exception is thrown first. However:
Your finally
s overwrite the exceptions thrown in the try blocks, so the finally
exceptions are the only ones seen by the exception handling code.
Don't throw or return in a finally block, for precisely this reason.
See the description of this problem in Google's Error Prone documentation for a lot more detail.
Upvotes: 4