What is try with resources?
Java try-with-resources is a special type of exception handling mechanism available in Java 7 and later that can automatically close resources such as FileInputStream, InputStream, and JDBC Connection.
The try-with-resources is an alternative and shortened construct of try-catch-finally block. You can achieve the same with try-catch-finally block but it is more specific and better than the old-technique.
You can use any object that implements java.lang.AutoCloseable as a resource, including all objects that implement java.io.Closeable.
Syntax
The try-with-resources construct is more improved in Java 9 and later versions, following are the two different syntaxes of it.
Java 7 and 8
Syntax of try-with-resources for Java 7 & 8.
try (<class_declare_and_init_1>; <class_declare_and_init_2>; <class_declare_and_init_N>) { // Work with the resources } catch (<exception_class> <object_name>) { // Handle the exception }
Java 9 and later
Syntax of try-with-resources for Java 9 and later.
<Class_declare_1> <object_1>; <Class_declare_2> <object_2>; <Class_declare_N> <object_N>; try (<object_1>; <object_2>; <object_N>) { // Work with the resources } catch (<exception_class> <object_name>) { // Handle the exception }
Example
Following are the different examples of try-with-resources, try and modify them to better understand.
Old School Style
First, see the old mechanism with try-catch-block
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TryCatchFinallyExample { public static void main(String[] args) throws IOException { FileReader reader = null; try { // Open the file reader and read everything. reader = new FileReader("Sample.txt"); int ch; // Read the file while ((ch = reader.read()) != -1) { System.out.print((char) ch); } } catch (FileNotFoundException ex) { // Handle the exception System.out.println(ex); } finally { // Check and close the reader if (reader != null) { reader.close(); } } } }
Example for Java 7 & 8
In the following example program, The FileReader class is used to read all contents from the file (Sample.txt) once it is done the try-with-resource will automatically close it.
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TryWithResourcesExample1 { public static void main(String[] args) throws IOException { try (FileReader reader = new FileReader("Sample.txt")) { int ch; // Read the file while ((ch = reader.read()) != -1) { System.out.print((char) ch); } } catch (FileNotFoundException ex) { // Handle the exception System.out.println(ex); } } }
Example for Java 9 and later
The following example program is the improved version of the above program.
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TryWithResourcesExample2 { public static void main(String[] args) throws IOException { FileReader reader = new FileReader("Sample.txt"); // No need to declare and initialize, just pass the object try (reader) { int ch; // Read the file while ((ch = reader.read()) != -1) { System.out.print((char) ch); } } catch (FileNotFoundException ex) { // Handle the exception System.out.println(ex); } } }
Points to remember
- The try-with-resources will always close and release the resource even if an exception occurs.
- You can provide N number of resources with a single try-with-resources construct.
- The closing order of resource follows the first-in-last-out order. Means the resources that were defined first will be closed last.
- A try-with-resources block can also have the catch and finally block.
- In Java 7 and 8 a resource must be declared and initialized inside the try.