I was recently working on a code, which was failing without giving any exception. So I had to add lots of logging to the code, to see where is the bug. I caught nothing via this method.
So I decided to check out the module source code and yeah finally I found it while debugging. The module was correctly implementing what was required and generating the required Exception as expected.
The author, unfortunately, forgot one of the rules of the Java Language and was returning from a finally block, effectively discarding the exception that was launched just a couple of lines before.
That's amazing how many times I see people returning from a finally block without understanding what the code is intended to do.
Still not convinced?
Run this:
No exception is thrown. Surprised? You should not. There are plenty of variations of this theme out there. Nevertheless, if you're still surprised that the hello() method completes without throwing an UnsupportedOperationException, please read on.
You probably though many times about what happens if an exception is thrown in a finally block, don't you? Perhaps you even tested that case: the exception that will be propagated up the stack will be the exception launched in the finally block. That's not surprising, after all, and that's why you should carefully check that the code in finally blocks don't fail without control.
So far, so good. But what happens, then, when a return statement is executed inside a finally block?
That's why exceptions launched in try or catch blocks (the "reason" of their abrupt completion) are simply ignored and not relaunched when a finally block completes abruptly with a return statement, being that the "resulting" reason of the abrupt completion of the try statement, as seen in the previous section.
Do think twice (or more...) before using this construct: chances are you're doing a favor to others and yourself if you avoid it.
So I decided to check out the module source code and yeah finally I found it while debugging. The module was correctly implementing what was required and generating the required Exception as expected.
The author, unfortunately, forgot one of the rules of the Java Language and was returning from a finally block, effectively discarding the exception that was launched just a couple of lines before.
That's amazing how many times I see people returning from a finally block without understanding what the code is intended to do.
Still not convinced?
Run this:
@Test public void hello() { try { throw new UnsupportedOperationException(); } finally { return; } }
No exception is thrown. Surprised? You should not. There are plenty of variations of this theme out there. Nevertheless, if you're still surprised that the hello() method completes without throwing an UnsupportedOperationException, please read on.
try/catch/finally blocks
According to the Java Language Specification, this is a simplified vision of what happens during the execution of a finally block (you can read the normative documentation here):- If the finally block completes normally, then the try statement completes abruptly for reason R.
- If the finally block complete abruptly for reason S, then the try statement completes abruptly for reason S.
You probably though many times about what happens if an exception is thrown in a finally block, don't you? Perhaps you even tested that case: the exception that will be propagated up the stack will be the exception launched in the finally block. That's not surprising, after all, and that's why you should carefully check that the code in finally blocks don't fail without control.
So far, so good. But what happens, then, when a return statement is executed inside a finally block?
The return statement
What Java programmers often misunderstand is the very nature of a return statement. The Java Language Specification is clear about that:The return statement always completes abruptly.
That's why exceptions launched in try or catch blocks (the "reason" of their abrupt completion) are simply ignored and not relaunched when a finally block completes abruptly with a return statement, being that the "resulting" reason of the abrupt completion of the try statement, as seen in the previous section.
Lesson learned
I personally don't see any good reason, unless you want any exception to be swallowed, to return in a finally block: it defies the very reason for try/catch blocks to exist.Do think twice (or more...) before using this construct: chances are you're doing a favor to others and yourself if you avoid it.
No comments:
Post a Comment