Java 7 try-with-resources
Jump to navigation
Jump to search
External
- https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
- http://docs.oracle.com/javase/7/docs/technotes/guides/language/try-with-resources.html
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
- Can be used with streams: Java_8_Streams_API#From_Files