Try With Resources
The try-with-resources is a feature introduced in Java7. This is an exception handling technique, which helps to close the resources automatically, after the application gets completed. The use of try-with-resources removes the work of adding finally in your code for closing the resources.
An example of a resource can be a file or a connection object or a scanner object.
Any class that implements java.lang.AutoCloseable or java.io.Closeable also can be used with try-with-resources construct.
Exception Handling using try-catch-finally prior java7
Prior to Java7 while working with resources, you need to explicitly close the resource.
In the above example, the reader object is closed explicitly in the finally block.
Also, here if an exception occurs inside the try block and due to close() in finally block, then the exception from the try block will be suppressed and will show the exception from finally block only.
Exception Handling using try-with-resources in Java7
In Java7, try-with-resources feature allows you to close the resources automatically without using close() method. Let us modify the above construct using try-with-resources.
The first line of the code depicts the try-with-resources construct. The FileReader object is declared and instantiated inside the parentheses after the try keyword. Once the try block completes the reader object will be closed automatically.
Since the FileReader class implements the java.lang.AutoCloseable, this is acceptable as a resource.
If an exception is thrown, from try-with-resources block and from the try block, then the exception thrown from try-with-resources is suppressed.
The exception thrown by try only will be shown which is contradictory of Java6 exception handling. You can retrieve these suppressed exceptions by calling e.getSuppressed() method from inside the catch block.
Exception Handling using multiple resources
You can also use multiple resources inside a try-with-resources block and have them automatically closed.
Let us see the implementation
The above example creates two resources a FileReader and a FileWriter object inside the parentheses after the try keyword. Both the resources will be closed automatically after the completion of try block.
The resources will be closed in reverse order of the order in which they are created inside the brackets. In our example writer object will be closed first and then the reader object.
Classes that implement Closeable and AutoCloseable Interface
Any class that implements java.lang.AutoCloseable or any class that implements java.io.Closeable can be considered as a resource and can be used with try-with-resources construct.
An implementation of this
In the above example MyResource is a class which implements AutoCloseable and overrides close() method. myMethod() is the own method of MyResource class
Now this class can be invoked within try-with-resources construct
So with the use or try-with-resources, the code is safe in handling the exception and closing the resources without finally block.
That’s all. Happy Practicing.
Our Training Programs:
Bigdata Hadoop Training In Bangalore
Hibernate Training In Bangalore
Java Training In Bangalore
JEE Web Programming In Bangalore
Node.js Training In Bangalore
Struts2 Training In Bangalore
Web Designing In Bangalore