Java 7 Project Coin: try-with-resources explained with examples

Prior to “try-with-resources” (before Java 7) while dealing with SQL Statement or ResultSet or Connection objects or other IO objects one had to explicitly close the resource. So one would write something like-

try{
  //Create a resource- R
}catch(SomeException e){
  //Hanlde the exception
}finally{
  //if resource R is not null then
  try{
    //close the resource
  }
  catch(SomeOtherException ex){
  }
}

We have explicity close the resource and thereby few more lines of code. There are few cases where the developer will forget to close the resource. So to overcome these and other issues- try-with-resources is introduced in Java 7.

Lets look at an example of how we would use try..catch…finally in pre java 7-
Let me create 2 custom exceptions- ExceptionA and ExceptionB. These will be used throught out the examples.

public class ExceptionA extends Exception{
    public ExceptionA(String message){
        super(message);
    }
}

 

public class ExceptionB extends Exception{
    public ExceptionB(String message){
        super(message);
    }
}

Lets create some resource- say OldResource which has two methods- doSomeWork(): which does some work and close(): which does the closing [Note that this depicts the use of a generic resource- do some work and then close the resource]. Now each of these operations- doSomeWork, close- throw an exception each.

public class OldResource{
    public void doSomeWork(String work) throws ExceptionA{
        System.out.println("Doing: "+work);
        throw new ExceptionA("Exception occured while doing work");
    }
    public void close() throws ExceptionB{
        System.out.println("Closing the resource");
        throw new ExceptionB("Exception occured while closing");
    }
}

Lets use this resource in a sample program:

public class OldTry {
    public static void main(String[] args) {
        OldResource res = null;
        try {
            res = new OldResource();
            res.doSomeWork("Writing an article");

        } catch (Exception e) {
            System.out.println("Exception Message: "+e.getMessage()+" Exception Type: "+e.getClass().getName());
        }finally{
            try {
                res.close();
            } catch (Exception e) {
                System.out.println("Exception Message: "+e.getMessage()+" Exception Type: "+e.getClass().getName());
            }
        }
    }
}

The output:

Doing: Writing an article
Exception Message: Exception occured while doing work Exception Type: javaapplication4.ExceptionA
Closing the resource
Exception Message: Exception occured while closing Exception Type: javaapplication4.ExceptionB

The program is straight forward: create a new resource, use it, and then try to close it. One can look at the number of extra lines of code there. Now lets implement the same program using Java 7’s try-with-resource construct. For this we would need a new resource- NewResource. In Java 7 a new interface has been introduced- java.lang.AutoCloseable. Those resources which need to be closed implement this interface. All the older io APIs, socket APIs imlement Closeable interface- which means these resources can be closed. With Java 7- java.io.Closeable implements AutoCloseable. So everything works without breaking any existing code:)

The NewResource code below:

public class NewResource implements AutoCloseable{
    String closingMessage;

    public NewResource(String closingMessage) {
        this.closingMessage = closingMessage;
    }

    public void doSomeWork(String work) throws ExceptionA{
        System.out.println(work);
        throw new ExceptionA("Exception thrown while doing some work");
    }
    public void close() throws ExceptionB{
        System.out.println(closingMessage);
        throw new ExceptionB("Exception thrown while closing");
    }

    public void doSomeWork(NewResource res) throws ExceptionA{
        res.doSomeWork("Wow res getting res to do work");
    }
}

Now lets use the NewResource in a sample Program using try-with-resource:

public class TryWithRes {
    public static void main(String[] args) {
        try(NewResource res = new NewResource("Res1 closing")){
            res.doSomeWork("Listening to podcast");
        }catch(Exception e){
            System.out.println("Exception: "+e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());

        }
    }
}

The output:

Listening to podcast
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA

One thing to note above is that the Exception thrown by the close is being supressed by the Exception being thrown by the worker method.

So you can right away notice the difference between the two implementations- one using try…catch…finally and the other using try-with-resource. In the example above only one resource is declared as used. One can declare and use multiple resources within the try block, also nest these try-with-resources blocks. Along with this few new methods and constructor were added to the java.lang.Throwable class- all related to suppressing the exceptions thrown along with other exception. Best example for this would be- ExceptionA being thrown by the try block will get suppressed by the ExceptionB being thrown by the finally (while closing the resource) and this was the behavior pre Java 7, but with Java 7, the thrown exception keeps track of the exceptions it suppressed on its way to being caught/handled. So the earlier said example can be restated as follows- The ExceptionB being thrown by the close method gets added to the List of suppressed exception of the ExceptionA which is being thrown by the try block. Let me explain you- Nested try-with-resources and Supressed exceptions with the examples:

Nested try-with-resources

public class TryWithRes {
    public static void main(String[] args) {
        try(NewResource res = new NewResource("Res1 closing");
            NewResource res2 = new NewResource("Res2 closing")){
            try(NewResource nestedRes = new NewResource("Nestedres closing")){
                nestedRes.doSomeWork(res2);
            }
        }catch(Exception e){
            System.out.println("Exception: "+e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
        }

    }
}

The output for the above would be:

Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA

Note the order of closing the resources- latest first. Also note the exception being thrown by each of these close() operations is supressed.
Lets see how we can retrieve the supressed exceptions:
Suppressed Exceptions

public class TryWithRes {
    public static void main(String[] args) {
        try(NewResource res = new NewResource("Res1 closing");
            NewResource res2 = new NewResource("Res2 closing")){
            try(NewResource nestedRes = new NewResource("Nestedres closing")){
                nestedRes.doSomeWork(res2);
            }
        }catch(Exception e){
            System.out.println("Exception: "+e.getMessage()+" Thrown by: "+e.getClass().getSimpleName());
            if(e.getSuppressed() != null){
                for(Throwable t : e.getSuppressed()){
                    System.out.println(t.getMessage()+" Class: "+t.getClass().getSimpleName());
                }
            }
        }

    }
}

The output for the above code:

Wow res getting res to do work
Nestedres closing
Res2 closing
Res1 closing
Exception: Exception thrown while doing some work Thrown by: ExceptionA
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB
Exception thrown while closing Class: ExceptionB

The getSuppressed() method is used to retrieve the exceptions supressed by the thrown exception. Also new constructor has been added to Throwable class which can be used to enable or disable the supression of exceptions- If disabled none of the suppressed exceptions are tracked.

6 thoughts on “Java 7 Project Coin: try-with-resources explained with examples”

  1. Hi,

    Could I know, Exception block is not being executed in this case.

    public class TryWithRes {
    public static void main(String[] args) {
    try(NewResource res = new NewResource(“Res1 closing”);
    NewResource res2 = new NewResource(“Res2 closing”)){
    try(NewResource nestedRes = new NewResource(“Nestedres closing”)){
    nestedRes.doSomeWork(res2);
    }
    }catch(Exception e){
    System.out.println(“Exception: “+e.getMessage()+” Thrown by: “+e.getClass().getSimpleName());
    }
    }
    }

    Reply
    • Its is getting executed for close in all the 3 resources. And also the exception being thrown by doSomeWork. But only the exception thrown by the doSomeWork is shown and the other exceptions are present in the supressed list of the thrown exception.

      Reply
  2. Where are Exception block is exected here in this example. what could be the reason here?
    Plese help on this….

    public static void main(String[] args) {
    try(NewResource res = new NewResource(“Res1 closing”);
    NewResource res2 = new NewResource(“Res2 closing”)){
    try(NewResource nestedRes = new NewResource(“Nestedres closing”)){
    nestedRes.doSomeWork(res2);
    }}catch(Exception e){ System.out.println(“Exception: “+e.getMessage()+” Thrown by: “+e.getClass().getSimpleName());
    if(e.getSuppressed() != null){
    for(Throwable t : e.getSuppressed()){
    System.out.println(t.getMessage()+” Class: “+t.getClass().getSimpleName());
    }
    }
    }
    }

    Reply
  3. Pingback: JavaPins

Leave a Reply

Discover more from Experiences Unlimited

Subscribe now to keep reading and get access to the full archive.

Continue reading