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.
No related posts.
5 Responses to Java 7 Project Coin: try-with-resources explained with examples
Connect to us …
Archives
- May 2013 (8)
- April 2013 (6)
- March 2013 (6)
- January 2013 (5)
- November 2012 (2)
- September 2012 (1)
- July 2012 (5)
- June 2012 (1)
- May 2012 (4)
- April 2012 (7)
- March 2012 (2)
- February 2012 (4)
- December 2011 (2)
- November 2011 (4)
- October 2011 (2)
- September 2011 (1)
- August 2011 (1)
- July 2011 (1)
- June 2011 (1)
- May 2011 (1)
- April 2011 (1)
- February 2011 (4)
- December 2010 (3)
- November 2010 (2)
- September 2010 (2)
- August 2010 (3)
- May 2010 (2)
- March 2010 (6)
- December 2009 (1)
- November 2009 (3)
- July 2009 (6)
- June 2009 (3)
- May 2009 (1)
- April 2009 (6)
- March 2009 (1)
- January 2009 (1)
- December 2008 (8)
- November 2008 (5)
- October 2008 (6)
- September 2008 (4)
- August 2008 (8)
- July 2008 (19)
- June 2008 (29)
- May 2008 (27)
- April 2008 (11)
- March 2008 (8)
- February 2008 (22)
- January 2008 (3)
Send To Readmill
Send to ReadmillRecent Posts
- How to create ADF TreeTable programmatically?
- Book review: The Object-Oriented Thought Process
- How to show links in ADF Messages
- Runtime Polymorphism in Java
- Understanding RowKey values in ADF TreeTable
- Train Wreck Pattern – A much improved implementation in Java 8
- Converting a List into comma separated value string in Java
- A simple application of Lambda Expressions in Java 8
- Template Method Pattern- Using Lambda Expressions, Default Methods
- First look at Learning Play! Framework 2
Disclaimer
Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for your support.










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());
}
}
}
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.
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());
}
}
}
}
The exception has been captured and stored in the Suppressed Exceptions list. So we retrieve them from that list and print it out to the user.
Java 7 Project Coin: try-with-resources explained with examples…
Thank you for submitting this cool story – Trackback from JavaPins…