...
This noncompliant code example uses an ordinary try-finally block to try to close two resources. However, if closing the Bufferedreader
BufferedReader
br
results in an exception being thrown, then the BufferedWriter
bw
will not be closed.
...
Code Block | ||
---|---|---|
| ||
public void err54_nce2(String inPath) throws IOException{ BufferedReader br = null; try { br = new BufferedReader(new FileReader(inPath)); // process the input and produce the output } finally { if (br != null) { br.close(); } } } |
Compliant Solution
...
This compliant solution uses a try-with-resources statement which will not suppress any exceptions thrown during the processing of the input while still guaranteeing that br
is closed.
...