Java try-with-resources

Java 7 supports a new statement called try-with-resources which extends the behavior of the traditional try/catch block for the sake of automatic resource management, since Java 7 developers are able to access resources (files, db connections, sockets) inside a try-with-resources block without the need to worry about closing them afterwards, the resource closure is done automatically.

1. Resource management using traditional try/catch

Before Java 7, the traditional way of accessing a resource in a java application is through surrounding it with a try/catch block and closing it manually in a finally block as the following:

This approach used to cause memory leaks and resource exhaustion in case developers forget to close the used resources in a finally block.

2. Resource management using try-with-resources

Java 7 introduced a new resource management approach called try-with-resources which closes the resources automatically after usage. Following is the structure of the try-with-resources statement:

P.S: It is worth to mention that in order for the resource to be auto managed by try-with-resources, it should extend AutoCloseable class, here is a list of all auto close-able classes.

3. Read file from file system

Following is a practical comparison between try/catch/finally and try-with-resources approaches for reading and displaying the content of a file in a file system.

using try/catch/finally

As you notice, too much manual interaction and handling in the above approach:

  • Developer should manually close the InputStream in a finally block.
  • When closing the InputStream, it’s possible that the close() method throws exception, then developer should keep handling the failure of closing in an infinite try/catch/finally blocks.

using try-with-resources

The above problems are automatically handled when using try-with-resources statement, below is how we read and display file contents in Java 7:

4. Advantages of try-with-resources

Below are the main benefits of preferring try-with-resources over the traditional try/catch block:

  1. Resources are closed automatically after usage without the need for developer interaction.
  2. In the traditional try/catch block, the exceptions which occur in the finally block override the exceptions which occur in the try block, hence only finally block exceptions are propagated. However, in try-with-resources, exceptions thrown when closing resources are suppressed and the exception thrown in the try block is considered.
  3. Try-with-resources reduces the boiler plate code and makes your code look shorter and more readable.

5. Common points on try-with-resources

Following are common points to consider when using try-with-resources:

  1. Only AutoCloseable resources can be defined in the try() statement.
  2. The scope of the resources defined in the try() statement is only limited to the try-with-resources block and can’t be used outside it.
  3. Multiple resources can be defined in the try() statement, and the close methods of resources are automatically called in the opposite order of their creation.

    here writer.close() is implicitly called before br.close().
  4. Catch and finally blocks can still be used in a try-with-resources statement, they are run after the resources declared have been closed.
  5. Try-with-resources only close the resources defined in the try() statement, it doesn’t close the resources defined afterwards inside the try block.
  6. Exceptions thrown while implicitly closing the resources are suppressed and only exceptions which occur in the try block are propagated.

 

husseinterek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
purnima das Recent comment authors
newest oldest most voted
purnima das
Guest
purnima das

Good, thanks for posting this article. Java Try with Resources well explained.