Java finally block

Java finally block is the block that is executed always whether the exception is raised or not and handled or not(if exception raised).

It is recommended to write ‘clean up’ code inside finally block so that we are making sure resources got released which were created and used in try block. ‘clean up’ code is such as closing File, database connection, network connection, stream … etc.

Java finally block follows either try or catch block.

It is not mandatory to write finally block always. If we created and used any resources (like connection, stream, file … etc.) in try block, then only we need finally block to close them.

It is never recommended to write ‘clean up’ code in try block because there is no guarantee for the execution of every statement in try block.

It is never recommended to write ‘clean up’ code in catch block because if there is no exception then catch block won’t execute.

For each try block there can be zero or more catch blocks, but only one finally block allowed.

Java finally block – exception handling

Syntax of try-finally block

Syntax of try-catch-finally block

Example 01 : try-catch-finally block – no exception raised

Output :

main begin
try block : Risky code
result: 1
finally block : Always executes
main end

In the above example, catch block is not executed because exception is not raised. But finally block executed.

Example 02 : try-catch-finally block – exception raised

Output :

main begin
try block : Risky code
catch-block : Handling ArithmeticException
finally block : Always executes
main end

In the above example, catch block executed because exception is raised and also finally block executed.

Example 03 : try-catch-finally block – unhandled exception raised

Output :

main begin
try block : Risky code
finally block : Always executes
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception08.finallyblock.Eample03FinallyBlockUnhandledException.main(Eample03FinallyBlockUnhandledException.java:10)

In the above example, we have coded to handle NullPointerException, But ArithmeticException raised in try block and program got terminated abnormally. But still finally block executed.

Example 04 : try-finally block – no exception raised

Output :

main begin
try block : Risky code
1
finally block : Always executes
main end

Here exception not raised, finally block executed. Generally if we don’t want to handle the exception and not care about rest of code execution, then we can avoid catch block.. But it is not recommended approach.

Example 05 : try-finally block – exception raised

Output :

main begin
try block : Risky code
finally block : Always executes
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception08.finallyblock.Eample05TryFinallyBlockExceptionRaised.main(Eample05TryFinallyBlockExceptionRaised.java:10)

In the above example, we don’t have catch block that means we don’t have handling code. As ArithemeticException raised, the rest of the program not executed. But still finally block executed. It is always recommended to have catch block to make sure rest of the code executes fine.

Example6 : finally block – not executes when system exits

Output :

main begin
try block : Risky code
1

In the above example, finally block not executed as system exits. Only in this case finally block won’t execute.

Example7 : finally block – real example of cleanup code

Scroll to Top