The finally Block
Since the finally block executes regardless of an exception in the try block, it is helpful in more ways than one. The finally block can be used as a tool to prevent resource leaks. The code to close a file or any resources can be placed in a finally block to ensure that the job is done. It also prevents the cleanup code from bypassing the return, break or continue statements. It’s a good practice to implement the finally block, even when no exceptions are anticipated.
When used in different cases, the finally block results in different outputs. Let us discuss them in brief and understand their use.
Case 1: There is no exception in the code
When the try block encounters no exception in its code, the try block is executed and then the finally block. The catch block, if it exists, is ignored.
try
{
int a = 10, b = 5;
int Result = a/b;
System.out.println("Result = " + Result);
}
catch
{
System.out.println("The operation is not possible with b = 0");
}
finally
{
System.out.println("END");
}

You can also try this code with Online Java Compiler
Run Code
Output:
Result = 2
END
A return statement in the try block
A subroutine uses a return statement to return to the method that called it. If the try block contains a return statement and has a finally block following it, the finally block is first executed, and then the control returns to the calling method.
public static void main(String[] args) {
division();
System.out.println("-----------------");
}
public static void division() {
try
{
int a = 10, b = 5;
int Result = a/b;
System.out.println("Result = " + Result);
return;
}
finally
{
System.out.println("END");
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Result = 2
END
----------------
A System.exit(0) statement in the try block
The System.exit(0) statement, unlike the return statement, completely exits the program. It means that the finally block will not be executed as well. However, if an exception occurs in the System.exit(0) method, then the finally block gets executed.
try
{
int a = 10, b = 5;
int Result = a/b;
System.out.println("Result = " + Result);
System.exit(0);
}
finally
{
System.out.println("END");
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Result = 5
Case 2: Exception is handled by the catch block
Now, let us assume that the try block finds an exception in its code. The catch block that contains the code to be executed on encountering an exception is chosen. If a finally block exists after the catch block, it is executed.
try
{
int a = 10, b = 0;
int Result = a/b;
System.out.println("Result = " + Result);
}
catch
{
System.out.println("The operation is not possible with b= 0");
}
finally
{
System.out.println("END");
}

You can also try this code with Online Java Compiler
Run Code
Output:
The operation is not possible with b = 0
END
Case 3: Exception in finally block
The finally block does not execute when there is an exception within the block itself. The exception encountered can be handled using another try-catch block within the finally block.
int a = 10, b = 5;
try
{
int Result = a*b;
System.out.println("Product = " + Result);
}
finally
{
System.out.println("Quotient = " + (a/b));
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
Product = 50
Exception in thread "main" java.lang.ArithmeticException: / by zero
Practice by yourself on java online compiler.
In conclusion, the finally block is not executed in the following cases:
- Due to the death of a thread.
- Due to the System.exit(0) statement in the try block.
- The presence of an exception in the finally block.
Must Read Conditional Statements in Java and Hashcode Method in Java
Frequently Asked Questions
Where to use finally?
The finally block is used when you want to execute some code no matter what—whether an exception occurs or not. It's ideal for closing files, releasing resources, or cleaning up tasks that must always run after try-catch blocks.
How does the try {} catch {} finally work?
In a try-catch-finally setup, the try block runs first. If an exception occurs, control moves to the catch block. Regardless of whether an exception was caught or not, the finally block always executes—making it perfect for clean-up actions.
Why do we use finally?
We use finally to ensure that crucial code always runs, like releasing memory, closing connections, or stopping background tasks. It adds reliability to your code by guaranteeing certain actions are completed, even if an exception interrupts the normal flow.
Can we use try with finally?
Yes, you can use try with just a finally block—without a catch. This is useful when you don’t need to handle the exception but still want some clean-up code to run, like closing a file or disconnecting from a database.
Conclusion
At some point, every developer encounters exceptions—it's a normal part of coding. Using the finally block in Java is considered a reliable way to handle clean-up actions, making your code more robust. In this blog, we’ll explore the basics of exception handling and take a closer look at how the finally block in Java works. We’ve also included practical examples to show how it can be used effectively in different scenarios.
Recommended Readings: