...
Allowing checked exceptions to escape a finally
block also violates rule ERR04-J. Do not exit abruptly from a finally block.
...
This compliant solution encloses the close()
method invocation in a try-catch
block of its own within the finally
block. Consequently, the potential IOException
can be handled without permitting allowing it to propagate further.
Code Block | ||
---|---|---|
| ||
public class Operation { public static void doOperation(String some_file) { // ... code to check or set character encoding ... try { BufferedReader reader = new BufferedReader(new FileReader(some_file)); try { // Do operations } finally { try { reader.close(); } catch (IOException ie) { // Forward to handler } // ... Other clean-up code ... } } catch (IOException x) { // Forward to handler } } } |
...
Code Block | ||
---|---|---|
| ||
public class Operation {
public static void doOperation(String some_file) {
// ... code to check or set character encoding ...
try ( // try-with-resources
BufferedReader reader =
new BufferedReader(new FileReader(some_file))) {
// Do operations
} catch (IOException ex) {
System.err.println("thrown exception: " + ex.toString());
Throwable[] suppressed = ex.getSuppressed();
for (int i = 0; i < suppressed.length; i++) {
System.err.println("suppressed exception: "
+ suppressed[i].toString());
}
// Forward to handler
}
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Please supply a path as an argument");
return;
}
doOperation(args[0]);
}
}
|
When an IOException
occurs in the try
block of the doOperation()
method, it is caught by the catch
block and printed as the thrown exception. This includes both any exceptions while doing operations and any exceptions incurred while that occur while creating the BufferedReader
. When an IOException
occurs while closing the reader
, that exception is also caught by the catch
block and printed as the thrown exception. When If both the try
block and closing the reader
throw an IOException
, the catch
clause catches both exceptions and prints the try
block exception as the thrown exception. The close exception is suppressed and printed as the suppressed exception. In all cases the reader
is safely closed.
...
Failure to handle an exception in a finally
block can lead to may have unexpected results.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR05-J | low | unlikely | medium | P2 | L3 |
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="aac7f8a9b128a70c-40997972-4a6a4465-95c88ca2-c7b17906206864d94f1fb162"><ac:plain-text-body><![CDATA[ | [[Bloch 2005 | AA. Bibliography#Bloch 05]] | Puzzle 41. Field and Stream | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fae9386aff8785e9-03f47264-4d7945ac-bf3b8d82-f2d38dac99f375151673b87c"><ac:plain-text-body><![CDATA[ | [[Chess 2007 | AA. Bibliography#Chess 07]] | 8.3, Preventing Resource Leaks (Java) | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="2cafba6523638da6-9b1fa080-4d1d4061-a53083b1-bcee54bff2977a6d64fabc35"><ac:plain-text-body><![CDATA[ | [[Harold 1999 | AA. Bibliography#Harold 99]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8fee506b223e15aa-fe6bfa3f-4e084fec-834fb614-8f3f3cc49d4d1b672bb1e5e9"><ac:plain-text-body><![CDATA[ | [[J2SE 2011 | AA. Bibliography#J2SE 11]] | The try-with-resources Statement | ]]></ac:plain-text-body></ac:structured-macro> |
...