Skip to content
Related Articles

Related Articles

Why You Need to Close the Java Streams in Finally Block?
  • Last Updated : 02 Feb, 2021

In Java finally block is a block used to execute important and common code. The finally block is mostly used during exception handling with try and catch to close streams and files. The code in finally block is executed irrespective of whether there is an exception or not. This ensures that all the opened files are properly closed and all the running threads are properly terminated. So, the data in the files will not be corrupted and the user is on the safe-side.

Finally block is executed after try and catch block. The flow of the code is:

  • try
  • catch
  • finally

Below mentioned are two examples of when exception is caused and when it is not caused.

We will observe that finally block is executed in both our codes.

Example 1: With exception in code



Java




// Java program to show the execution of the code
// when exception is caused
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        try
            
            // open files
            System.out.println("Open files");
            
            // do some processing
            int a = 45;
            int b = 0;
            
            // divinding by 0 to get an exception
            int div = a / b;
            
            System.out.println("After dividing a and b ans is " + div);
        }
  
        catch (ArithmeticException ae) {
            System.out.println("exception caught");
  
            // display exception details
            System.out.println(ae);
        }
  
        finally {
            System.out.println("Inside finally block");
  
            // close the files irrespective of any exception
            System.out.println("Close files");
        }
    }
}
Output
Open files
exception caught
java.lang.ArithmeticException: / by zero
Inside finally block
Close files

Example 2: Without exception

Java




// Java program to show the execution of the code
// when exception is not caused
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        try
            
            // open files
            System.out.println("Open files");
            
            // do some processing
            int a = 45;
            int b = 5;
            
            int div = a / b;
            System.out.println("After dividing a and b ans is " + div);
        }
  
        catch (ArithmeticException ae) {
            
            System.out.println("exception caught");
  
            // display exception details
            System.out.println(ae);
        }
  
        finally {
            
            System.out.println("Inside finally block");
  
            // close the files irrespective of any exception
            System.out.println("Close files");
        }
    }
}
Output
Open files
After dividing a and b ans is 9
Inside finally block
Close files

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 :