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