Short answer
try-with-resources auto-closes any AutoCloseable declared in its header, in reverse order of acquisition, generating the close() calls for you.
Explanation
It's better than a finally block because manual finally cleanup is verbose and, critically, an exception thrown by close() in a finally can suppress the original exception from the try body — try-with-resources instead attaches close() failures as suppressed exceptions (getSuppressed()), so you never lose the root cause. It also correctly handles multiple resources without nested finallys.
Common mistakes
- Forgetting resources close in reverse order of declaration.
- Using try-with-resources on objects that are not AutoCloseable.