Home > Guides > Programming > Java

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Try-With-Resources

Last updated Jun 17, 2011.

One thing that has always been tedious for Java developers is cleaning up resources that are contained in a try block. Consider the standard example of executing a database query: it would be nice if we could write that code as follows:

try { 
  Connection connection = ds.getConnection(); 
  PreparedStatement ps = connection.prepareStatement( "SELECT * FROM TABLE" ); 
  ResultSet rs = ps.executeQuery(); 
  while( rs.next() ) { 
    ... 
  } 
  rs.close(); 
  ps.close(); 
  connection.close(); 
} 
catch( Exception e ) { 
  e.printStackTrace(); 
}

Of course, what is the big problem with this code? The problem is that if there is a problem with the connection, the prepared statement, or the result set then these resources do not get properly closed. We then find ourselves having to close these resources in a finally block to ensure that whether or not the code succeeds, the resources are closed. But to further complicate things, closing each of these resource can generate exceptions themselves. This turn this simple operation into the following "code overhead":

Connection connection = null; 
PreparedStatement ps = null; 
ResultSet rs = null; 
 
try { 
  Connection connection = ds.getConnection(); 
  PreparedStatement ps = connection.prepareStatement( "SELECT * FROM TABLE" ); 
  ResultSet rs = ps.executeQuery(); 
  while( rs.next() ) { 
    ... 
  } 
  rs.close(); 
  ps.close(); 
  connection.close(); 
} 
catch( Exception e ) { 
  e.printStackTrace(); 
} 
finally { 
  try { 
    if( rs != null ) { 
      rs.close(); 
    } 
  } 
  catch( Exception e ) { 
    e.printStackTrace(); 
  } 
 
  try { 
    if( ps != null ) { 
      ps.close(); 
    } 
  } 
  catch( Exception e ) { 
    e.printStackTrace(); 
  } 
 
  try { 
    if( connection != null ) { 
      connection.close(); 
    } 
  } 
  catch( Exception e ) { 
    e.printStackTrace(); 
  } 
}

While this works, it is incredibly messy: not only do you have to close your resources but you have to create them outside the try block, close them in their own try-catch blocks, but you also have to check their state before you do so.

Java 7 introduces what is referred to as try-with-resources, which allows you to define the resources that need to be closed within the try construct itself and then those resources will be automatically closed for you when the try block completes, regardless of whether or not an exception occurred.

The try-with-resources command works as follows:

try { Create objects that can be automatically closed here ) {
    //Do stuff ...
}
catch( Exception e ) {
   // Handle Exception
}

Add parentheses to the end of the try statement and create objects that can be automatically closed within those parentheses. You can still handle exceptions and even have a finally block, but you do not need to close the resources that are automatically closed. You can create a single object or multiple objects, separating each by a semi-colon.

Looking back at our database connection challenge, here is that same code written using a try-with-resources:

try( Connection connection = ds.getConnection(); 
     PreparedStatement ps = connection.prepareStatement( "SELECT * FROM TABLE" ); 
     ResultSet rs = ps.executeQuery() ) { 
  while( rs.next() ) { 
    ... 
  } 
} 
catch( Exception e ) { 
  e.printStackTrace(); 
} 

The connection, prepared statement, and result set are all created inside the try command and then the body of the try block iterates over the results. When the block completes, the JVM automatically closes them on your behalf.

The way that this is accomplished is through the java.lang.AutoClosable interface. Any class that implements this interface can be used inside a try-with-resources block and will be closed when the try block is complete when the JVM executes the class's close() method (the only method defined by the AutoCloseable interface.) In Java 7 there are several classes that implement the AutoCloseable interface (see the Javadoc link above), but the ones of note are:

  • All of the InputStream, OutputStream, Reader, and Writer derivations
  • The database classes: Connection, Statement, PreparedStatement, ResultSet, RowSet, JdbcRowSet
  • Most of the socket related classes
  • Most of the audio (Java Sound) classes

And of course if you want to create your own class that can be used in such a way, the answer is to implement the AutoCloseable interface and provide an implementation of the close() method.

If you want to follow along at home, listing 1 shows a sample class that uses the new try-with-resources command and listing 2 shows the Maven POM file that builds it. Note that you will need to setup Java 7 in your environment and add it to your PATH before your operating system finds your current Java version.

Listing 1. TryWithResourcesExample.java

package com.javasrc.informit.trywithresourcesexample;

import java.sql.*;

public class TryWithResourcesExample
{
    public static void main( String[] args )
    {
        try( Connection conn = DriverManager.getConnection( "jdbc:hsqldb:file:testdb", "SA", "" );
             PreparedStatement ps = conn.prepareStatement( "SELECT * FROM MYTABLE" );
             ResultSet rs = ps.executeQuery() )
        {
            while( rs.next() )
            {
                // ...
            }
        }
        catch( SQLException e )
        {
            e.printStackTrace();
        }
    }
}
Listing 2. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.javasrc.informit</groupId>
    <artifactId>TryWithResourcesExample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>TryWithResourcesExample</name>
    <url>http://maven.apache.org</url>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

In terms of features that make your development life easier, even if just for convenience and clean code, the new try-with-resources has to be at the top of the list for Java 7.

Related Resources

Rachel BaylessDay 9 of #17DaysofGiveaways - Software Engineering Week
By Rachel BaylessJuly 19, 2012Comments

The Day 9 giveaway will add a nice touch to your bookshelf. We’re giving away sets of “GEEK” bookends from Etsy seller Knob Creek Metal Arts. They are perfect for keeping your software engineering books together!

Rachel BaylessDay 7 of #17DaysofGiveaways - Software Engineering Week
By Rachel BaylessJuly 17, 2012Comments
Java Application Architecture: Modularity Patterns with Examples Using OSGi by Kirk Knoernschild is today’s eBook giveaway. Knoernschild brings a one-of-a-kind perspective to Java application architecture, focusing on the way modularity will change the development of Java applications.
Rachel BaylessDay 4 of #17DaysofGiveaways - Web Development Week
By Rachel BaylessJuly 12, 2012Comments

It’s Day 4 of InformIT’s 17 Days of Giveaways. Visit yesterday’s blog for a recap of the first three day’s prizes. Today’s prize is an eBook copy of Programming in CoffeeScript by Mark Bates. Bates begins with the basics, delivers tips and techniques throughout, and finishes off with the practical applications of CoffeeScript. He teaches you how to use the robust toolset to create code, free of the bugs of JavaScript. Comment on this post for your chance to win your own copy.

See More Blogs