Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a finally block without a catch block a java anti-pattern?

I just had a pretty painful troubleshooting experience in troubleshooting some code that looked like this:

try {    doSomeStuff()    doMore() } finally {    doSomeOtherStuff() } 

The problem was difficult to troubleshoot because doSomeStuff() threw an exception, which in turn caused doSomeOtherStuff() to also throw an exception. The second exception (thrown by the finally block) was thrown up to my code, but it did not have a handle on the first exception (thrown from doSomeStuff()), which was the real root-cause of the problem.

If the code had said this instead, the problem would have been readily apparent:

try {     doSomeStuff()     doMore() } catch (Exception e) {     log.error(e); } finally {    doSomeOtherStuff() } 

So, my question is this:

Is a finally block used without any catch block a well-known java anti-pattern? (It certainly seems to be a not-readily-apparent subclass of the obviously well-known anti-pattern "Don't gobble exceptions!")

like image 873
Jared Avatar asked Mar 02 '09 03:03

Jared


People also ask

Can we have finally block without catch block in Java?

Yes, it is not mandatory to use catch block with finally. You can have to try and finally.

Is it possible to have a finally block without catch?

Yes, It is possible to have a try block without a catch block by using a final block. As we know, a final block will always execute even there is an exception occurred in a try block, except System.

Is finally block mandatory with try-catch in Java?

Therefore, we will put code like closing connections, stream objects, etc. or any cleanup code in the finally block so that they can be executed even if an exception occurs. The finally block in Java is usually put after a try or catch block. Note that the finally block cannot exist without a try block.

Can we avoid finally block in Java?

You cannot skip the execution of the final block. Still if you want to do it forcefully when an exception occurred, the only way is to call the System. exit(0) method, at the end of the catch block which is just before the finally block.


2 Answers

In general, no, this is not an anti-pattern. The point of finally blocks is to make sure stuff gets cleaned up whether an exception is thrown or not. The whole point of exception handling is that, if you can't deal with it, you let it bubble up to someone who can, through the relatively clean out-of-band signaling exception handling provides. If you need to make sure stuff gets cleaned up if an exception is thrown, but can't properly handle the exception in the current scope, then this is exactly the correct thing to do. You just might want to be a little more careful about making sure your finally block doesn't throw.

like image 67
dsimcha Avatar answered Sep 21 '22 10:09

dsimcha


I think the real "anti-pattern" here is doing something in a finally block that can throw, not not having a catch.

like image 35
Logan Capaldo Avatar answered Sep 24 '22 10:09

Logan Capaldo