 
                            ...
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="bfd99d3ffc64e6a3-015215c8-428a4923-bded967b-f867b521f03572f58f9ab2f5"><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="6122d60d66548941-c24e4685-4e394d39-9cc4960e-9e3217a3116cfb7863536a67"><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="8c9546ec5259245f-77fc5276-49fe425b-b29a8ea7-efde323a1d0566972cfac11e"><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="a3f213dc38d73585-dfbcb908-4a194cb0-910db19b-86d75dc58af3df827945e8c5"><ac:plain-text-body><![CDATA[ | [[J2SE 2011 | AA. Bibliography#J2SE 11]] | The try-with-resources Statement | ]]></ac:plain-text-body></ac:structured-macro> | 
...