Why can a return or throw statement inside a finally block override a thrown exception or a return statement executed in the associated try or catch blocks?

John Mitchell

First off, we know that, by definition of the try/catch/finally statement (Section 14.19 of the JLS), the finally block is executed no matter what happens in the try/catch blocks.

Next, also by definition, both the return (Section 14.16) and throw (Section 14.17) statements complete abruptly.

Lastly, by definition, abrupt completion of a finally block (Section 14.19.2) causes the try/catch/finally statement to complete abruptly.

Therefore, executing a return or throw in the finally clause abruptly completes the finally clause which, in turn, abruptly completes the try statement which therefore has the effect of masking/overriding any previous return or throw statement executed in the associated try/catch blocks.

So, take a look at your try/catch/finally statements and check to see that you're actually handling exceptions and returns the way that you expected.