Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The close() method can throw an IOException which, if thrown, would prevent execution of any subsequent clean-up statements. The compiler correctly fails to diagnose this problem because IOException}}s are any IOException would be caught by the outer catch block. Also, an exception thrown from the {{close() operation can also mask any exception that gets thrown during execution of the "Do operations" block, preventing proper recovery.

...

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 it to propagate fartherfurther.

Code Block
bgColor#ccccff
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
    }
  }
}

...

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 also any exceptions incurred 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 both the try block and also 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.

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1a6ea6a193930686-e43bd513-48b44697-9d93802e-0827adc9402212708e55a238"><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="3ffa42e35a37274c-a8be009b-401848a5-bcaa8dac-2c5763c7fcf49641c1b205b3"><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="748f2cbb9aff1794-c4850550-4c5c4c18-bb4bbf47-df304f6adf1b6ae1eba61b6c"><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="537318aca38c82dc-bdff77c9-4bf841ca-954f86bd-3990adbdcd7d1fd433779382"><ac:plain-text-body><![CDATA[

[[J2SE 2011

AA. Bibliography#J2SE 11]]

The try-with-resources Statement

]]></ac:plain-text-body></ac:structured-macro>

...