Whats new in Java 7 – try-with-resource construct in Project Coin

In our previous post we looked at different features added as part of Project Coin, in this post we will look at another language construct added as part of Java 7 and Project Coin in particular. It is the try-with-resource construct also called as Automatic Resource Management. Before getting into the details of the construct, let me show you some basic code which I would be using through out this example.

also read:

Lets create a class called Resource which implements Closeable interface and its a dummy resource class something on liens of a FileReader or your other IO classes. Lets also have a custom exception say SomeException which extends Exception class. The code for the two is as follows:

class Resource implements Closeable{

  public String resourceName;
  public Resource(String resourceName) throws SomeException{
    this.resourceName = resourceName;
    System.out.println(this.resourceName+" created!
                                Might throw an exception");

  public void useResource() {
    System.out.println("Using the resource "+this.resourceName);

  public void close() throws IOException {
    System.out.println("Closing the resource "+this.resourceName);

class SomeException extends Exception{

Suppose I wanted to create an instance of Resource and use it, prior to the try-with-resource construct we would do something like:

Resource myResource = null;
  myResource = new Resource("POOR OLD RESOURCE");
}catch(SomeException ex){
}finally {
  if (myResource != null) {
    try {
    } catch (IOException e) {


POOR OLD RESOURCE created! Might throw an exception
Using the resource POOR OLD RESOURCE
Closing the resource POOR OLD RESOURCE

We generally clear all the resources in the finally block because that is the one which is called irrespective of the exception being thrown or not. But close() method in the Closeable interface itself throws an IOException which makes us to use another try..catch block in the finally method. Now, if both the try block and the catch block throw exception, prior to Java 7 the one thrown in the try block would have been suppressed by the one thrown in the catch block. In Java 7 there’s has been a new API added to Throwable which states- getSuppressedExeceptions. Let me not go into those details now. Coming back to our try-with-resources construct, the above code can be written concisely as:

try ( Resource myResource2 = new Resource("TRY-WITH-RESOURCE")){
}catch ( SomeException | IOException ex){


TRY-WITH-RESOURCE created! Might throw an exception
Using the resource TRY-WITH-RESOURCE
Closing the resource TRY-WITH-RESOURCE

Look at the reduction in the number of lines, and also it makes the code more readable. The construct:

try ( Resource myResource2 = new Resource("TRY-WITH-RESOURCE"))

is the new construct added as part of Project Coin. One can use and declare multiple resource within the try block.

try ( Resource myResource2 = new Resource("TRY-WITH-RESOURCE");
      Resource myResource3 = new Resource("TRY-WITH-RESOURCE2"))

The complete code can be downloaded from here.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>