J
Jim Janney
I'm reading up on Java 7's try-with-resource statement and looking at
the tutorial at
http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
which includes the following code:
As a matter of habit, I always write that pattern as
on the theory that if you reach that point br can never be null, so the
test is both redundant and confusing. On the other hand, I might be
wrong. Is there a reason to test for null in the finally block?
the tutorial at
http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
which includes the following code:
static String readFirstLineFromFileWithFinallyBlock(String path)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
if (br != null) br.close();
}
}
As a matter of habit, I always write that pattern as
static String readFirstLineFromFileWithFinallyBlock(String path)
throws IOException {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
}
on the theory that if you reach that point br can never be null, so the
test is both redundant and confusing. On the other hand, I might be
wrong. Is there a reason to test for null in the finally block?