...
| 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
}
}
}
|
Compliant Solution (Java
...
SE 7: try-with-resources)
Java 1.SE 7 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.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fe62db8208b40d91-b1babdee-46dc486e-aa6dadc7-11668ec8721a3e971879aa13"><ac:plain-text-body><![CDATA[ | [[Bloch 2005 | AA. References#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="6ca6ef62eb7407c6-bd496c24-44f64434-852a93e5-ee844b4ef4389fd44bca24a0"><ac:plain-text-body><![CDATA[ | [[Chess 2007 | AA. References#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="0b020388c90fba3b-16bdb475-49884612-9232bec5-0b85a89a3b0634ef0086b32f"><ac:plain-text-body><![CDATA[ | [[Harold 1999 | AA. References#Harold 99]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="08cef08595230c4f-c8cf610e-4d0044a2-89e7be5e-9a310754ce4bd271acc8c077"><ac:plain-text-body><![CDATA[ | [[J2SE 2011 | AA. References#J2SE 11]] | The try-with-resources Statement | ]]></ac:plain-text-body></ac:structured-macro> |
...