The try-with-resources statement, added in Java 7, declares one or more resources in parentheses after try and closes them automatically when the block ends, whether it finishes normally or throws. Every resource must implement java.lang.AutoCloseable (or its subinterface java.io.Closeable). It replaces the verbose finally blocks that used to close files, sockets, streams and JDBC connections by hand.
The compiler generates the closing code for you, closes resources in reverse order of declaration, and keeps the original exception even when close() also fails.
Before Java 7 you had to close every resource in a finally block, and the close() call itself could throw, so you needed a nested try:
This is easy to get wrong. If close() throws inside finally, it replaces the exception from the try body, so the real failure is lost. With several resources, one failing close() can prevent the others from being closed at all.
You declare the resource in the parentheses after try. The compiler wraps the body in the equivalent of a try/finally and calls close() for you.
The reader variable is implicitly final and is only in scope inside the try block. It is closed before any catch or finally clause runs, so those clauses never see an open resource.
AutoCloseable. Closeable extends it and is used by the I/O classes, with close() declared as throws IOException.final or effectively final, without redeclaring it.null resource is allowed: the compiler skips close() for it.Separate resources with semicolons. They are closed in the reverse order of declaration, so a wrapper is closed before the stream it wraps.
Here the ResultSet closes first, then the PreparedStatement, then the Connection. Note that there is no catch at all: a try-with-resources statement does not need one, because the throws clause can propagate the exception.
If the body throws and close() also throws, the body's exception is the one that propagates. The exception from close() is attached to it as a suppressed exception, which you can read with getSuppressed(). In the old finally approach the close() exception would have replaced the original one.
If only close() fails, that exception is thrown as a normal exception.
Any class can take part. Implement AutoCloseable, make close() safe to call more than once, and declare only the checked exceptions it can actually throw.
Since Java 19, ExecutorService also implements AutoCloseable, so thread pools can be managed the same way: close() waits for submitted tasks to finish.
| Aspect | try/finally | try-with-resources |
|---|---|---|
| Boilerplate | A nested try for close() | None |
| Closing order | You write it yourself | Reverse of declaration, guaranteed |
Exception from close() | Can hide the original exception | Added as a suppressed exception |
| Several resources | One failure can skip later closes | Each resource is still closed |
| Minimum Java version | Any | 7 (reuse of a variable: 9) |
Yes. The catch and finally clauses are optional. Without them, any exception, including one thrown by close(), propagates to the caller, so the method must declare or handle checked exceptions.
Nothing is closed and no NullPointerException is thrown. The compiler checks for null before calling close(). A NullPointerException can still occur if you use the resource inside the block.
Yes. The resources are closed whenever control leaves the block: normal completion, return, break, continue or an exception.
Yes. The AutoCloseable contract recommends it, and Closeable.close() requires that closing an already closed resource has no effect. Wrapped streams often call close() on the same underlying stream twice.
Use try-with-resources for every file, stream, socket, JDBC object and other closeable. It closes resources in reverse order, never loses the primary exception, and removes the finally boilerplate. Implement AutoCloseable on your own classes that hold external resources, and make close() safe to call repeatedly.