Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Use finally block for Catching Exceptions

  • Difficulty Level : Hard
  • Last Updated : 27 Nov, 2020

The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the connection. The finally block executes whether exception rise or not and whether exception handled or not. A finally contains all the crucial statements regardless of the exception occurs or not.

There are 3 possible cases where finally block can be used:

Case 1: When an exception does not rise 

In this case, the program runs fine without throwing any exception and finally block execute after the try block.

Java




// Java program to demonstrate
// finally block in java When
// exception does not rise 
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        try {
            System.out.println("inside try block");
            
            // Not throw any exception
            System.out.println(34 / 2);
        }
        
        // Not execute in this case
        catch (ArithmeticException e) {
            
            System.out.println("Arithmetic Exception");
            
        }
        // Always execute
        finally {
            
            System.out.println(
                "finally : i execute always.");
            
        }
    }
}
Output



inside try block
17
finally : i execute always.

Case 2: When the exception rises and handled by the catch block

In this case, the program throws an exception but handled by the catch block, and finally block executes after the catch block.

Java




// Java program to demonstrate finally block in java
// When exception rise and handled by catch
  
import java.io.*;
  
class GFG {
    
    public static void main(String[] args)
    {
        try {
            System.out.println("inside try block");
  
            // Throw an Arithmetic exception
            System.out.println(34 / 0);
        }
  
        // catch an Arithmetic exception
        catch (ArithmeticException e) {
  
            System.out.println(
                "catch : exception handled.");
        }
  
        // Always execute
        finally {  
            
          System.out.println("finally : i execute always.");
        }
    }
}
Output
inside try block
catch : exception handled.
finally : i execute always.

Case 3: When exception rise and not handled by the catch block 

In this case, the program throws an exception but not handled by catch so finally block execute after the try block and after the execution of finally block program terminate abnormally, But finally block execute fine.

Java




// Java program to demonstrate finally block 
// When exception rise and not handled by catch
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        try {
            System.out.println("Inside try block");
  
            // Throw an Arithmetic exception
            System.out.println(34 / 0);
        }
  
        // Can not accept Arithmetic type exception
        // Only accept Null Pointer type Exception
        catch (NullPointerException e) {
  
            System.out.println(
                "catch : exception not handled.");
        }
  
        // Always execute
        finally {
  
            System.out.println(
                "finally : i will execute always.");
        }
        // This will not execute
        System.out.println("i want to run");
    }
}

Output

Inside try block
finally : i will execute always.
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at GFG.main(File.java:10)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :