Finally Block In Java: In our previous post, we have discussed and learned about Exception Handling and how we can use the try-catch block. In this post, we are going to learn about the finally block and how to use the finally block with a try-catch block, and different scenarios and uses of finally with simple Java programs.
As we shared in our previous post, we need to put all the important statements in the Finally block, which should be executed whether an exception occurs or not, for example, closing a connection to database or file, etc.
Syntax of Finally block
try { //Statements that may cause an exception } catch { //Handling exception } finally { //Statements to be executed }
Finally Block example
class Example { public static void main(String args[]) { try { int num=121/0; System.out.println(num); } catch(ArithmeticException e) { System.out.println("Number should not be divided by zero"); } /* Finally block will always execute * even if there is no exception in try block */ finally { System.out.println("This is finally block"); } System.out.println("Out of try-catch-finally"); } }
Points to Remember Regarding Finally
- Finally, the block is optional because we can handle an exception with the use of the try-catch block.
- A Finally block must be associated with a try block that means in a program you can not write a finally block without a try block.
- Those statements are written inside the finally block that will be executed irrespective of exception occurs or not.
- The execution flow of finally block in the normal case is it will execute after the try block, and if there is some exception occurs, then the first try block catch block, and after that, finally, the block will be executed.
- If there is an exception occurs in the finally block, then it behaves like another exception, and you will get a system generated error msg for that.
- If there is any control transfer statements are present like return, break and continue in try block also, at last, the Finally block will be executed.
Finally Block with Return Statement
class JavaFinally { public static void main(String args[]) { System.out.println(JavaFinally.myMethod()); } public static int myMethod() { try { return 112; } finally { System.out.println("This is Finally block"); System.out.println("Finally block ran even after return statement"); } } }
Cases when the finally block doesn’t execute
There are also some circumstances in what time the finally block is executed, and the circumstances are below:
- The Death of an execution thread
- If there is a system.exit() method in any block
- If an exception occurs in the Finally block
How to Handle Exception Using Try Finally In Java Example?
package com.softwaretestingblog.programs; public class WithTryFinally { public static void main(String[] args) { // TODO Auto-generated method stub try { int i=9; int z=i/0; System.out.println("Executed Sucessfully"+ z); } finally { System.out.println("Exception Comes"); } } }
Output:
Exception Comes Exception in thread "main" java.lang.ArithmeticException: / by zero at com.softwaretestingblog.programs.WithTryFinally.main(WithTryFinally.java:8)
How to Use Try, Catch & Finally-Exception With Java Program?
package com.softwaretestingblog.programs; public class WithTryCatchFinally { public static void main(String[] args) { // TODO Auto-generated method stub try { int i=8; int z=i/0; System.out.println("Executed Sucessfully"+z); } catch(ArithmeticException e) { System.out.println(e.getMessage()); } finally { System.out.println("Finally Block Executed"); } } }
Output:
/ by zero Finally Block Executed
Leave a Reply