Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I using the Java 7 try-with-resources correctly

I am expecting the buffered reader and file reader to close and the resources released if the exception is throw.

public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException {     try (BufferedReader br = new BufferedReader(new FileReader(filePath)))     {         return read(br);     }  } 

However, is there a requirement to have a catch clause for successful closure?

EDIT:

Essentially, is the above code in Java 7 equivalent to the below for Java 6:

public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException {      BufferedReader br = null;      try     {         br = new BufferedReader(new FileReader(filePath));          return read(br);     }     catch (Exception ex)     {         throw ex;     }     finally     {         try         {             if (br != null) br.close();         }         catch(Exception ex)         {         }     }      return null; } 
like image 763
Cheetah Avatar asked Jul 15 '13 09:07

Cheetah


People also ask

What is the difference between try and try with resources?

The try -with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try -with-resources statement ensures that each resource is closed at the end of the statement.

How does Java 7 handle closing of resources?

Support for try-with-resources — introduced in Java 7 — allows us to declare resources to be used in a try block with the assurance that the resources will be closed after the execution of that block. The resources declared need to implement the AutoCloseable interface.

Does try with resources automatically close?

The Java try with resources construct, AKA Java try-with-resources, is an exception handling mechanism that can automatically close resources like a Java InputStream or a JDBC Connection when you are done with them. To do so, you must open and use the resource within a Java try-with-resources block.


1 Answers

It's correct and there's no requirement for catch clause. Oracle java 7 doc says the resource will be closed regardless of whether an exception is actually thrown or not.

You should use a catch clause only if you want to react upon the exception. The catch clause will be executed after the resource is closed.

Here's a snippet from Oracle's tutorial:

The following example reads the first line from a file. It uses an instance of BufferedReader to read data from the file. BufferedReader is a resource that must be closed after the program is finished with it:

static String readFirstLineFromFile(String path) throws IOException {     try (BufferedReader br =                    new BufferedReader(new FileReader(path))) {         return br.readLine();     } } // In this example, the resource declared in the try-with-resources statement is a BufferedReader. 

... Because the BufferedReader instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly (as a result of the method BufferedReader.readLine throwing an IOException).

EDIT

Regarding the new edited question:

The code in Java 6 executes the catch and afterwards the finally block. This causes the resources to be still potentially opened in the catch block.

In Java 7 syntax, resources are closed before the catch block, so resources are already closed during the catch block execution. This is documented in the above link:

In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

like image 150
yair Avatar answered Oct 05 '22 18:10

yair