Finally block

We know that whenever exception occurs, we need to handle such exception using try/catch

But, If we miss to handle such exception then program terminates abruptly.

This skips some of the important statements to be executed in the program.

So, in such scenario, finally blocks comes to rescue us.

All the statements or a piece of code we write in finally block executes always no matter whether exception occurs or not.

So, we must keep any important piece of code to be executed inside finally block.

Example: Closing resources like database connection, stream.

Syntax of finally block


Case 1: with catch block

  1. try {
  2.     //Piece of code that may cause an exception
  3. }
  4. catch {
  5.    //Handling exception
  6. }
  7. finally {
  8.    //Statements to be executed always
  9. }
try {
    //Piece of code that may cause an exception
}
catch {
   //Handling exception
}
finally {
   //Statements to be executed always
}


Case 2: without catch block

  1. try {
  2.     //Piece of code that may cause an exception
  3. }
  4. finally {
  5.    //Statements to be executed always
  6. }
try {
    //Piece of code that may cause an exception
}
finally {
   //Statements to be executed always
}


Example :


Case 1 : Exception does not occur

  1. class FinallyBlockExample1{  
  2.   public static void main(String args[]){  
  3.   try{  
  4.    int result=10/5;  
  5.    System.out.println(result);  
  6.   }  
  7.   catch(ArithmeticException e){
  8. System.out.println(e);
  9. }  
  10.   finally{
  11. System.out.println("finally block is always executed");
  12. }  
  13.   System.out.println("continuing further flow...");  
  14.   }  
  15. }  
class FinallyBlockExample1{  
  public static void main(String args[]){  
  try{  
   int result=10/5;  
   System.out.println(result);  
  }  
  catch(ArithmeticException e){
System.out.println(e);
}  
  finally{
System.out.println("finally block is always executed");
}  
  System.out.println("continuing further flow...");  
  }  
}  


Case 2 : Exception occurs but not handled

  1. class FinallyBlockExample2{  
  2.   public static void main(String args[]){  
  3.   try{  
  4.    int result=10/0;  
  5.    System.out.println(result);  
  6.   }
  7.   finally{
  8. System.out.println("finally block is always executed");
  9. }  
  10.   System.out.println("continuing further flow...");  
  11.   }  
  12. }  
class FinallyBlockExample2{  
  public static void main(String args[]){  
  try{  
   int result=10/0;  
   System.out.println(result);  
  } 
  finally{
System.out.println("finally block is always executed");
}  
  System.out.println("continuing further flow...");  
  }  
}  


Case 3 : Exception occurs and its handled

  1. class FinallyBlockExample3{  
  2.   public static void main(String args[]){  
  3.   try{  
  4.    int result=10/0;  
  5.    System.out.println(result);  
  6.   }  
  7.   catch(ArithmeticException e){
  8. System.out.println(e);
  9. }  
  10.   finally{
  11. System.out.println("finally block is always executed");
  12. }  
  13.   System.out.println("continuing further flow...");  
  14.   }  
  15. }  
class FinallyBlockExample3{  
  public static void main(String args[]){  
  try{  
   int result=10/0;  
   System.out.println(result);  
  }  
  catch(ArithmeticException e){
System.out.println(e);
}  
  finally{
System.out.println("finally block is always executed");
}  
  System.out.println("continuing further flow...");  
  }  
}  


We can see that, in all the above scenarios finally block is executed always no matter whether exception has occurred or not.

So, we should always prefer using finally block to execute important code irrespective of whether exception occurs or not.

When finally, block does not gets executed ?


1) Whenever current thread terminates

2) When we use system.exit() method

3) Exception occurred in finally block and not handled then finally block does not execute further.

Finally block with system.exit()


System.exit() statement behaves differently than return statement. Unlike return statement whenever System.exit() gets called in try block then finally block doesn’t execute at all.

Example :

  1. try {
  2.  
  3.    System.out.println("Inside try block");
  4.    System.exit(0);
  5. }
  6. catch (Exception e) {
  7.    System.out.println(e);
  8. }
  9. finally {
  10.    System.out.println("finally block");
  11. }
try {
  
   System.out.println("Inside try block");
   System.exit(0);
}
catch (Exception e) {
   System.out.println(e);
}
finally {
   System.out.println("finally block");
}


In the above program, finally block does not get executed as there is system.exit(0) in the try block.

Key Points on finally
Finally block should be associated with try block, It means we can not use finally block without try block. Finally block is not mandatory but good to have to execute important piece of code Finally block executes after try block if no exception and it executes after catch block if there is any exception and its handled. Finally block executes even if there is a return statement in try block Exception might be thrown again within finally block and it is same as any other exception behavior

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on