Monday, 24 February 2014

try with Resources Statement

A resource is any thing that the program use in execution and must be closed once the program finished with it. Example, A FileInputStream object, FileOutputStream object etc.,

The try-with-resources statement ensures that each resource is closed at the end of the statement.

Syntax
try( resource1;
         resource2;
         …...
         resurce N ) {
}

Create input.txt file in the same folder, where your program resides

Example
import java.io.*;

class TryEx{
 public static void main(String args[]){
  try(
   FileInputStream fin = new FileInputStream("input.txt");
   FileOutputStream fout = new FileOutputStream("input.txt");
  ){
   for(int i = 97; i < 110; i++)
    fout.write(i);
   int b;
   
   while( (b = fin.read()) != -1){
    System.out.print((char)b +" ");
   }
  }
  catch(Exception e){
  }
 }
}
  
Output
a b c d e f g h i j k l m

In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

In the above program, FileInputStream, FileOutputStream are used inside the try with resource statement, which implements the AutoClosable interface, So these resources closed once the try finished execution either normally or abruptly.

Try-with-resource statement doesn't need to have a catch or finally block associated with. The resurces closes in the reverse order.

Example
import java.io.*;

class MyResource implements AutoCloseable{
 String name;

 MyResource(String name){
  this.name = name;
 }

 public void close(){
  System.out.println("Closing Resource " + name);
 }
}

import java.io.*;

class SuppressEx{
 public static void main(String args[])throws Exception{
  try(
   MyResource obj1 = new MyResource("Resource 1");
   MyResource obj2 = new MyResource("Resource 2");){
  }
 }
}
Output
Closing Resource Resource 2
Closing Resource Resource 1

As you see the program, Resource 1 is created first, and Resource 2 next. But while closing the resources closed in the reverse order. In java the resource is anything that implements the AutoCloseable interface. And observe that, abve program doesn't associate any catch or finally for the try with resource block.

Some points to remember
1. A try with resource statement can have catch, finally block associated with. But these are executed once the resources created by try with resource closed.

Example
import java.io.*;

class MyResource implements AutoCloseable{
 String name;

 MyResource(String name){
  this.name = name;
 }

 public void close(){
  System.out.println("Closing Resource " + name);
  throw new RuntimeException("I am RuntimeException for the resource " + name);
 }
}

import java.io.*;

class SuppressEx{
 public static void main(String args[])throws Exception{
  try(
   MyResource obj1 = new MyResource("Resource 1");
   MyResource obj2 = new MyResource("Resource 2");){
  }
  catch(Exception e){
   System.out.println("I am in catch block " + e);
  }
  finally{
   System.out.println("I am in finally block");
  }
 }
}
  
Output
Closing Resource Resource 2
Closing Resource Resource 1
I am in catch block java.lang.RuntimeException: I am RuntimeException for the resource Resource 2
I am in finally block

As you observe the output, An exception thrown while closing the resource 2, so the catch block associated with the try-with-resource statement executed and after that finally executed.

throw vs throws                                                 autocloseable interface                                                 Home

No comments:

Post a Comment