Java 7 try-with-resources

From NovaOrdis Knowledge Base
Revision as of 18:36, 12 June 2021 by Ovidiu (talk | contribs) (→‎Overview)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

External

Internal

Overview

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement, regardless of whether the sequence completes successfully or an exception is thrown. The programmer does not need to explicitly close resources.

Local variables declared as resources in a try-with-resources statement are implicitly final.

try (BufferedReader br = new BufferedReader(new FileReader(file))) {

    // ...

}
catch(IOException e) {

    // ...

}

AutoCloseable

TODO