Home » Java Basics » try/catch/finally » try with resource example

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

try with resource example

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());

  }
    }
}


Output:

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


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());

  }
 
    }
}


Output:

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 that the exception being thrown by each of these close() operations is suppressed.

Lets see how we can retrieve the suppressed 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());


    }


}

  }
 
    }
}


Output:

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

Related Article:

Reference: Java 7 Project Coin: try-with-resources explained with examples from our JCG partner Mohamed Sanaulla at the Experiences Unlimited blog

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of