A resource is an object that must be closed once the program is finished or done using it, for example, file resource, JDBC database connection resource, socket connection resource etc.

Prior to Java 7, there was no auto resource management, we open the resource in a try block and close the resource in finally block. For example:

try{
//open resources
}catch(IOException){
//Exception handling
}finally{
//close resource
}

In the above approach there were few drawbacks:

  1. It causes memory leaks and performance issue if the developer forgot to close the resource.
  2. Finally block will always be executed, it doesn’t matter if the exception is thrown from the try block. It means, it tries to close the resource in finally block, even if it doesn’t exist which again cause an exception to be thrown from finally block.
  3. Exception thrown from finally block will be propagated up the call stack, though exception from try block is more relevant.

try-with-resource
In Java 7, a new approach called “try-with-resources” is introduced for auto resource management. In this when try block finishes, it automatically closes the resource.

Here is an example for try-with-resource construct:

private static void readFile() throws IOException {
try(FileInputStream input = new FileInputStream("file.txt")) {
int data = input.read();
while(data != -1){
System.out.print((char) data);
data = input.read();
}
}
}

 

try-with-resource using multiple resources
We can use multiple resources in try-with-resource block, it will automatically close both the resources when program finish executing try block.

private static void readFile() throws IOException {
try(  FileInputStream input = new FileInputStream("file.txt");
BufferedInputStream bufferedInput = new BufferedInputStream(input)
) {
int data = bufferedInput.read();
while(data != -1){
System.out.print((char) data);
data = bufferedInput.read();
}
}
}

The resources are closed in reverse order of they are declared inside the try. BufferedInputStream will be closed first, then FileInputStream.
 
AutoCloseable Custom implementation
Java 7 has introduced an interface java.lang.AutoCloseable. To use any resource in try-with-resource block, it must implement the AutoCloseable interface. We can also implement java.lang.AutoCloseable interface in our own classes and use them with the try-with-resources construct.
AutoCloseable interface only has one method class close(). Here is the AutoCloseable interface:

public interface AutoCloseable {
public void close() throws Exception;
}

 
Exception handling with try-with-resource:
If one or more exceptions are thrown by try block in try-with-resources, the exceptions thrown are suppressed exception.
Java added an constructor and two methods in Throwable class to deal with suppressed exceptions. We can get these exceptions by using the getSuppress() method of Throwable class.

Advantages of using try-with-resource construct:

  1. Auto resource management.
  2. More readable code.
  3. No need of finally block just to close the resource.
  4. Meaning & relevant exceptions.

Related Articles

Leave a Reply

Be the First to Comment!

avatar