Does it make sense to do "try-finally" without "catch"?

JavaException

Java Problem Overview


I saw some code like this:

	try
	{
		db.store(mydata);
	}
	finally
	{
		db.cleanup();
	}

I thought try is supposed to have a catch?

Why does this code do it this way?

Java Solutions


Solution 1 - Java

This is useful if you want the currently executing method to still throw the exception while allowing resources to be cleaned up appropriately. Below is a concrete example of handling the exception from a calling method.

public void yourOtherMethod() {
    try {
        yourMethod();
    } catch (YourException ex) {
        // handle exception
    }
}    

public void yourMethod() throws YourException {
    try {
        db.store(mydata);
    } finally {
        db.cleanup();
    }
}

Solution 2 - Java

It's there because the programmer wanted to make sure that db.cleanup() is called even if the code inside the try block throws an exception. Any exceptions will not be handled by that block, but they'll only be propagated upwards after the finally block is executed. The finally block will also be executed if there was no exception.

Solution 3 - Java

> Why does this code do it this way?

Because apparently the code doesn’t know how to handle exceptions at this level. That’s fine – as long as one of the callers does, i.e. as long as the exception gets ultimately handled somewhere.

Often, low-level code cannot react appropriately to exceptions because the user needs to be notified, or the exception must be logged, or another strategy has to be tried. Low-level code performs one function only and doesn’t know about higher-level decision making.

But the code still needs to clean up its resources (because if it doesn’t, they would leak), so it does just that in the finally clause, making sure that it always happens, whether an exception was thrown or not.

Solution 4 - Java

The finally block ensures that even when a RuntimeException is thrown (maybe due to some bug in the called code), the db.cleanup() call will be made.

This is also often used to prevent too much nesting:

try
{
    if (foo) return false;
    //bla ...
    return true;
}
finally
{
    //clean up
}

Especially when there are many points at which the method returns, this improves readability as anyone can see the clean up code is called in every case.

Solution 5 - Java

The code is doing it to ensure that the database is closed.
Usually, the way you would do it is to put all your database accessing code in the try block, and then put a call to close the database in the finally block.
The way try...finally works, means that the code in the try block is run, and the code in the finally block is run when that finishes...no matter what.
Short of the computer being yanked from the wall, the finally will execute.
This means that even if an exception is called, and the method takes three years to execute, it will still go in the finally block and the database will be closed.

Solution 6 - Java

If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it's bubbled out of the method.

The finally block is always executed, whether an exception is thrown or not.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionglandenView Question on Stackoverflow
Solution 1 - JavaTaylor LeeseView Answer on Stackoverflow
Solution 2 - JavaMatti VirkkunenView Answer on Stackoverflow
Solution 3 - JavaKonrad RudolphView Answer on Stackoverflow
Solution 4 - JavaFRotthoweView Answer on Stackoverflow
Solution 5 - JavachustarView Answer on Stackoverflow
Solution 6 - JavaTHINESH VASEEView Answer on Stackoverflow