What happens if a catch block or finally blocks an exception?

In exception handling, what happens if a catch block or finally throws an Exception?

+6
source share
2 answers

Finally, a block exception will mask the original exception.

When a new exception is thrown in the catch block or, finally, the block that will be propagated from this block, the current exception will be interrupted (and forgotten) because the new exception is thrown out.

Check here and here for more details.

+5
source

According to JLS 14.20.2. Executing try-finally and try-catch-finally

If the catch block terminates abruptly for reason R, then the finally block is executed. Then there is a choice:

If the finally block completes normally, then the try statement terminates abruptly for reason R.

If the finally block finally completes for reason S, then the try statement suddenly terminates for reason S (and reason R is discarded).

The finally block can throw an exception, and if so, any exception thrown by the try or catch block is lost.

Link: http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.2

0
source

All Articles