• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

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

May 4, 2012 //  by Mohamed Sanaulla//  Leave a Comment

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:

  • Java 7.0 Tutorials
  • New Features in Java 7.0
  • G1 Garbage Collector in Java 7.0

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

  @Override
  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;
try{
  myResource = new Resource("POOR OLD RESOURCE");
  myResource.useResource();
}catch(SomeException ex){
  ex.printStackTrace();
}finally {
  if (myResource != null) {
    try {
      myResource.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Output:

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")){
  myResource2.useResource();
}catch ( SomeException | IOException ex){
  ex.printStackTrace();
}

Output:

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.

Category: JavaTag: Java, java 7, project coin

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Previous Post: « Using JsonSlurper Groovy API from Java to parse JSON
Next Post: Creating JSON document using Java and GSON API »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact