Methods invoked from within a finally
block can throw an exception. Failure to catch and handle such exceptions results in the abrupt termination of the entire try
block. This causes any exception thrown in the try
block to be forgottenlost, preventing any possible recovery method from handling that specific problem. Additionally, the transfer of control associated with the exception may prevent execution of any expressions or statements that occur after the point in the finally
block from which the exception is thrown. Consequently, programs must appropriately handle checked exceptions that are thrown from within a finally
block.
...
The close()
method can throw an IOException
which, if thrown, would prevent execution of any subsequent clean-up statements. The compiler will correctly fail fails to diagnose this problem because IOException}}s are 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 section" block, preventing proper recovery.
...
Compliant Solution (Java 1.7: try-with-resources)
Java 1.7 provides introduced a new feature, called try-with-resources, that can close certain resources automatically in the event of an error. This compliant solution uses try-with-resources to properly close the file.
...
When an IOException
occurs in the try
block of the doOperation()
method it will be is caught by the catch block and be 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 will is also be caught by the catch block and will be 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="822010aca101c4af-6247dc75-491a4d77-a8d59f59-74864b24cd2254f7775dd72c"><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="880edb526e5fed7c-a0cd0b71-414940fb-b1f79e00-c19bafaee52423a00c6bbe9f"><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="d969e145bf903513-cf05ea32-472a4502-a96ba0c4-698bf7e9f8337ea8ed81a359"><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="aebcddd2e791d4b1-4298ce2b-4a4d46a8-8f6a9ee4-e4885cf505b68bb56291de2d"><ac:plain-text-body><![CDATA[ | [[J2SE 2011 | AA. Bibliography#J2SE 11]] | The try-with-resources Statement | ]]></ac:plain-text-body></ac:structured-macro> |
...