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.

Syntax of try-finally block
1 2 3 4 5 6 |
try { // Resources created // Risky code } finally { // Resource releasing code } |
Syntax of try-catch-finally block
1 2 3 4 5 6 7 8 9 |
try { // Resources created // Risky code } catch (Exception e) { // Handling code } finally { // Resource releasing code } |
Example 01 : try-catch-finally block – no exception raised
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.vidvaan.corejava.exception08.finallyblock; //try-catch-finally block – no exception raised public class Eample01FinallyBlockNoExceptionRaised { public static void main(String[] args) { System.out.println("main begin"); try { System.out.println("try block : Risky code"); int result = 5 / 5; System.out.println("result: " + result); } catch (ArithmeticException e) { System.out.println("catch-block : Handling ArithmeticException"); } finally { // finally executes as exception not raised System.out.println("finally block : Always executes"); } System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.vidvaan.corejava.exception08.finallyblock; //try-catch-finally block - exception raised public class Eample02FinallyBlockExceptionRaised { public static void main(String[] args) { System.out.println("main begin"); try { System.out.println("try block : Risky code"); int result = 5 / 0; System.out.println("result: " + result); } catch (ArithmeticException e) { System.out.println("catch-block : Handling ArithmeticException"); } finally { // finally executes even exception raised System.out.println("finally block : Always executes"); } System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.vidvaan.corejava.exception08.finallyblock; //try-catch-finally block - unhandled exception raised public class Eample03FinallyBlockUnhandledException { public static void main(String[] args) { System.out.println("main begin"); try { System.out.println("try block : Risky code"); System.out.println(10 / 0); } catch (NullPointerException e) { System.out.println("catch-block : Handling NullPointerException"); } finally { // finally executes as unhandled exception raised System.out.println("finally block : Always executes"); } System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.vidvaan.corejava.exception08.finallyblock; //try-finally block - no exception raised public class Eample04TryFinallyNoExceptionRaised { public static void main(String[] args) { System.out.println("main begin"); try { System.out.println("try block : Risky code"); System.out.println(5 / 5); } finally { // finally executes as exception not raised System.out.println("finally block : Always executes"); } System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.vidvaan.corejava.exception08.finallyblock; //try-finally block - exception raised public class Eample05TryFinallyBlockExceptionRaised { public static void main(String[] args) { System.out.println("main begin"); try { System.out.println("try block : Risky code"); System.out.println(5 / 0); } finally { // finally executes even exception raised System.out.println("finally block : Always executes"); } System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.vidvaan.corejava.exception08.finallyblock; //finally block - not executes when system exits public class Eample06FinallyBlockNotExecuted { public static void main(String[] args) { System.out.println("main begin"); try { System.out.println("try block : Risky code"); System.out.println(5 / 5); System.exit(0); } catch (NullPointerException e) { System.out.println("catch-block : Handling NullPointerException"); } finally { // finally block not executes when it exits System.out.println("finally block : Always executes"); } System.out.println("main end"); } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.vidvaan.corejava.exception08.finallyblock; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; // finally block example clean up code public class Example07CleanUpCode { public static void main(String[] args) { InputStreamReader streamreader = null; BufferedReader bufferreader = null; String name = null; try { streamreader = new InputStreamReader(System.in); bufferreader = new BufferedReader(streamreader); System.out.println("Enter a Name ::"); name = bufferreader.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { // Example cleanup code try { streamreader.close(); bufferreader.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("User Name :: " + name); } } |