Why You Need to Close the Java Streams in Finally Block? Last Updated : 27 Oct, 2021 Comments Improve Suggest changes Like Article Like 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: trycatchfinally 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"); } } } OutputOpen 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"); } } } OutputOpen files After dividing a and b ans is 9 Inside finally block Close files Comment More infoAdvertise with us Next Article PrintStream close() method in Java with Examples S shubhigupta22 Follow Improve Article Tags : Java java-stream Practice Tags : Java Similar Reads PrintStream close() method in Java with Examples The close() method of PrintStream Class in Java is used to close the stream. Closing a stream deallocates any value in it or any resources associated with it. The PrintStream instance once closed won't work. Also a PrintStream instance once closed cannot be closed again. Syntax: public void close() 2 min read PrintStream close() method in Java with Examples The close() method of PrintStream Class in Java is used to close the stream. Closing a stream deallocates any value in it or any resources associated with it. The PrintStream instance once closed won't work. Also a PrintStream instance once closed cannot be closed again. Syntax: public void close() 2 min read StringReader close() method in Java with Examples The close() method of StringReader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. 2 min read StringReader close() method in Java with Examples The close() method of StringReader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. 2 min read LongStream.Builder accept() method in Java LongStream.Builder accept(long t) is used to insert an element into the element in the building phase of stream. It accepts an element to the stream being built. Syntax: void accept(long t) Parameters: This method accepts a mandatory parameter t which is the element to input into the stream. Excepti 2 min read Reader close() method in Java with Examples The close() method of Reader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. If an 2 min read Like