1. Overview
In this tutorial, we’ll explore the finally keyword in Java. We’ll see how to use it alongside try/catch blocks in error handling. Though finally is intended to guarantee the execution of code, we’ll discuss exceptional situations in which the JVM does not execute it.
We’ll also discuss some common pitfalls where a finally block can have an unexpected outcome.
2. What Is finally?
finally defines a block of code we use along with the try keyword. It defines code that’s always run after the try and any catch block, before the method is completed.
The finally block executes regardless of whether an exception is thrown or caught.
2.1. A Quick Example
Let’s look at finally in a try-catch-finally block:
try {
System.out.println("The count is " + Integer.parseInt(count));
} catch (NumberFormatException e) {
System.out.println("No count");
} finally {
System.out.println("In finally");
}
In this example, regardless of the value of the parameter count, the JVM executes the finally block and prints “In finally”.
2.2. Using finally Without a catch Block
Also, we can use a finally block with a try block regardless of whether a catch block is present:
try {
System.out.println("Inside try");
} finally {
System.out.println("Inside finally");
}
And we’ll get the output:
Inside try
Inside finally
2.3. Why finally Is Useful
We generally use the finally block to execute clean up code like closing connections, closing files, or freeing up threads, as it executes regardless of an exception.
Note: try-with-resources can also be used to close resources instead of a finally block.
3. When finally Is Executed
Let’s have a look at all the permutations of when the JVM executes finally blocks, so we can understand it better.
3.1. No Exception Is Thrown
When the try block completes, the finally block is executed, even if there was no exception:
try {
System.out.println("Inside try");
} finally {
System.out.println("Inside finally");
}
In this example, we aren’t throwing an exception from the try block. Thus, the JVM executes all code in both the try and finally blocks.
This outputs:
Inside try
Inside finally
3.2. Exception Is Thrown and Not Handled
If there’s an exception and it is not caught, the finally block is still executed:
try {
System.out.println("Inside try");
throw new Exception();
} finally {
System.out.println("Inside finally");
}
The JVM executes the finally block even in the case of an unhandled exception.
And the output would be:
Inside try
Inside finally
Exception in thread "main" java.lang.Exception
3.3. Exception Is Thrown and Handled
If there’s an exception and it is caught by the catch block, the finally block is still executed:
try {
System.out.println("Inside try");
throw new Exception();
} catch (Exception e) {
System.out.println("Inside catch");
} finally {
System.out.println("Inside finally");
}
In this case, the catch block handles the thrown exception, and then the JVM executes the finally block and produces the output:
Inside try
Inside catch
Inside finally
3.4. Method Returns from try Block
Even returning from the method will not prevent finally blocks from running:
try {
System.out.println("Inside try");
return "from try";
} finally {
System.out.println("Inside finally");
}
Here, even though the method has a return statement, the JVM executes the finally block before handing the control over to the calling method.
We’ll get the output:
Inside try
Inside finally
3.5. Method Returns from catch Block
When the catch block contains a return statement, the finally block is still called:
try {
System.out.println("Inside try");
throw new Exception();
} catch (Exception e) {
System.out.println("Inside catch");
return "from catch";
} finally {
System.out.println("Inside finally");
}
When we throw an exception from the try block, the catch block handles the exception. Though there is a return statement in the catch block, the JVM executes the finally block before handing control over to the calling method, and it outputs:
Inside try
Inside catch
Inside finally
4. When finally Isn’t Executed
Although we always expect the JVM to execute the statements inside a finally block, there are some situations where the JVM will not execute a finally block.
We might already expect that if the operating system stops our program, then the program would not get the chance to execute all of its code. There are also some actions we can take that will similarly prevent the execution of a pending finally block.
4.1. Invoking System.exit
In this case, we are terminating the JVM by calling System.exit and hence the JVM will not execute our finally block:
try {
System.out.println("Inside try");
System.exit(1);
} finally {
System.out.println("Inside finally");
}
This outputs:
Inside try
4.2. Invoking halt
Similar to System.exit, a call to Runtime.halt also halts the execution and the JVM does not execute any finally blocks:
try {
System.out.println("Inside try");
Runtime.getRuntime().halt(1);
} finally {
System.out.println("Inside finally");
}
Thus, the output will be:
Inside try
4.3. Daemon Thread
If a Daemon thread enters the execution of a try/finally block and all other non-daemon threads exit before the daemon thread executes the finally block, the JVM doesn’t wait for the daemon thread to finish the execution of finally block:
Runnable runnable = () -> {
try {
System.out.println("Inside try");
} finally {
try {
Thread.sleep(1000);
System.out.println("Inside finally");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread regular = new Thread(runnable);
Thread daemon = new Thread(runnable);
daemon.setDaemon(true);
regular.start();
Thread.sleep(300);
daemon.start();
In this example, the runnable prints “Inside try” as soon as it enters the method and waits for 1 second before printing “Inside finally”.
Here, we start the regular thread and the daemon thread with a small delay. When the regular thread executes the finally block, the daemon thread is still waiting within the try block. As the regular thread completes execution and exits, the JVM also exits and does not wait for the daemon thread to complete the finally block.
Here’s the output:
Inside try
Inside try
Inside finally
4.4. JVM Reaches an Infinite Loop
Here’s a try block which contains an infinite while loop:
try {
System.out.println("Inside try");
while (true) {
}
} finally {
System.out.println("Inside finally");
}
Though it’s not specific to finally, it’s worth mentioning that if the try or catch block contains an infinite loop, the JVM will never reach any block beyond that loop.
5. Common Pitfalls
There are some common pitfalls that we must avoid when we use the finally block.
Although it’s perfectly legal, it’s considered bad practice to have a return statement or throw an exception from a finally block, and we should avoid it at all costs.
5.1. Disregards Exception
A return statement in the finally block ignores an uncaught exception:
try {
System.out.println("Inside try");
throw new RuntimeException();
} finally {
System.out.println("Inside finally");
return "from finally";
}
In this case, the method ignores the RuntimeException thrown and returns the value “from finally”.
5.2. Ignores Other return Statements
A return statement in the finally block ignores any other return statement in the try or catch block. Only the return statement in the finally block executes:
try {
System.out.println("Inside try");
return "from try";
} finally {
System.out.println("Inside finally");
return "from finally";
}
In this example, the method always returns “from finally” and completely ignores the return statement in the try block. This could be a very difficult bug to spot, which is why we should avoid using return in finally blocks.
5.3. Changes What’s Thrown or Returned
Also, in the case of throwing an exception from a finally block, the method disregards the exception thrown or return statements in the try and catch blocks:
try {
System.out.println("Inside try");
return "from try";
} finally {
throw new RuntimeException();
}
This method never returns a value and always throws a RuntimeException.
While we may not intentionally throw an exception from the finally block as in this example, we may still encounter this issue. It can occur when cleanup methods we use in a finally block throw an exception.
6. Conclusion
In this article, we discussed what finally blocks do in Java and how to use them. Then, we looked at different cases where the JVM executes them, and a few when it might not.
Lastly, we looked at some common pitfalls associated with using finally blocks.
As always, the source code used in this tutorial is available over on GitHub.