ProAnswers.org

Try/Catch and Finally in Java?

If I have a try/catch block with returns inside it, will the finally block be called?

For example:

try {
something();
return success;
}
catch (Exception e)
{
return failure; }
finally {
System.out.println(“i don’t know if this will get printed out.”);
}

I know I can just type this in an see what happens (which is what I’m about to do, actually) but when I googled for answers nothing came up, so I figured I’d throw this up as a question.

finally will be called.

The only time finally won’t be called is if you call System.exit() or if the JVM crashes first.