Content deleted Content added
GreenC bot (talk | contribs) Rescued 1 archive link; reformat 1 link. Wayback Medic 2.5 per Category:All articles with dead external links |
|||
Line 469:
} finally {
// Always run when leaving the try block (including finally clauses), regardless of whether any exceptions were thrown or whether they were handled.
//
// May not be run when System.exit() is called and in other system-wide exceptional conditions (e.g. power loss).
// Rarely used after try-with-resources was added to the language (see below).
}
</syntaxhighlight>
A try statement may be followed with statements in a parenthesis. Resources acquired in these statements will be cleaned up automatically instead of in a finally block. Classes used in these statements must implement an interface called AutoCloseable.<ref>https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html</ref>
<syntaxhighlight lang="java">
try (FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr)) {
// Normal execution path.
} catch (IOException ioe) {
// Deal with exception.
}
// File and stream in the try statement are automatically closed afterwards.
</syntaxhighlight>
|