I am using Java 7 try-with-resources correctly

I expect that the buffered file reader and reader will close and resources will be released if an exception is thrown.

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

However, is there a need to have a catch clause for a successful close?

EDIT:

Essentially, the above code in Java 7 is equivalent to the one 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; } 
+56
java-7 try-with-resources
Jul 15 '13 at 9:32
source share
2 answers

That's right, and there is no requirement for a catch clause. Oracle java 7 doc says the resource will be closed regardless of whether the exception was actually thrown.

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

Here is an excerpt from an Oracle tutorial :

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

 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. 

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

EDIT

Regarding the new edited question:

Code in Java 6 executes a catch , and then a finally block. This causes resources to still potentially open in the catch .

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

The try-with-resources statement executes any catch or finally block after the declared resources have been closed.

+77
Jul 15 '13 at 9:40
source share
— -

In this particular case, your use of try-with-resources will work fine, but overall this is not entirely correct. You should not bind such resources, because it can lead to unpleasant surprises. Suppose you have a buffer variable size:

 public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException { int sz = /* get buffer size somehow */ try (BufferedReader br = new BufferedReader(new FileReader(filePath), sz)) { return read(br); } } 

Suppose something went wrong and you ended up with sz negative. In this case, your file resource (created using new FileReader(filePath) ) will be closed by NOT .

To avoid this problem, you must specify each resource separately as follows:

 public static Object[] fromFile(String filePath) throws FileNotFoundException, IOException { int sz = /* get buffer size somehow */ try (FileReader file = new FileReader(filePath); BufferedReader br = new BufferedReader(file, sz)) { return read(br); } } 

In this case, even if br initialization failed file , it still closes. You can find more information here and here .

+57
Jan 25 '14 at 9:37
source share



All Articles