Open In App

Why You Need to Close the Java Streams in Finally Block?

Improve
Improve
Like Article
Like
Save
Share
Report

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;
           
            // dividing 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

 



Last Updated : 27 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads