try-with-resources (AutoCloseable)
Use try-with-resources and AutoCloseable to manage resources safely.
Intro
Resources like files or sockets need closing. try-with-resources closes them automatically if they implement AutoCloseable.
AutoCloseable concept
Any class that implements AutoCloseable can be used. Common ones include FileReader, BufferedReader, and JDBC connections.
All lessons in this course
- Throwing & Custom Exceptions
- try-with-resources (AutoCloseable)
- Defensive Programming