ReviseAlgo Logo

Exception Handling

try-with-resources

Master AutoCloseable resource management, reverse closing sequences, and suppressed exception tracking.

Interview: Highly tested: AutoCloseable close() execution sequence, suppressed exceptions (getSuppressed), and try-with-resources vs. try-finally.

Last Updated: June 13, 2026 10 min read

The try-with-resources statement (introduced in Java 7) simplifies resource management by automatically closing resources that implement AutoCloseable at the end of the block.

AutoCloseable

Any resource declared in the try parentheses must implement the AutoCloseable or Closeable interface.

Reverse Closure

Resources are closed automatically in the reverse order of their declaration, preventing dependency issues during closure.

Suppressed Errors

If try body and close() both throw exceptions, the close() exception is suppressed to prevent it from hiding the primary exception.

How Suppressed Exceptions Work

In legacy try-finally blocks, if an exception occurred in the try block, and another occurred during close() in the finally block, the second exception would propagate, swallowing the primary exception.

The try-with-resources statement resolves this issue:

  • Primary Exception: The exception thrown from the try block body is treated as the primary exception and propagates to the caller.
  • Suppressed List: Any exception thrown during the automatic close() calls is caught and attached to the primary exception as a suppressed exception.
  • Retrieval: Callers can inspect suppressed exceptions using the Throwable.getSuppressed() method.

Common Pitfalls

  • Omitting AutoCloseable: Trying to declare a class that does not implement AutoCloseable in the try-with-resources statement, resulting in a compile error.
  • Modifying variables: Reassigning a resource variable inside the try body, which is prohibited because resource variables are implicitly final.

Best Practices

  • Prefer try-with-resources: Always use try-with-resources instead of try-finally to manage resources like streams, sockets, and database connections.
  • Implement AutoCloseable on Custom Resources: Implement AutoCloseable on custom classes that manage resources (like temporary file pools) to ensure automatic cleanup.

Interview-Relevant Information

Q1: What interface must a class implement to be declared in try-with-resources?
Answer: The class must implement either java.lang.AutoCloseable or java.io.Closeable.

Q2: How are suppressed exceptions retrieved?
Answer: By calling the getSuppressed() method on the primary exception object. This returns an array of Throwable objects representing the exceptions thrown during resource closure.

Quick Checklist

Can you write a try-with-resources block, explain the order in which resources are closed, and describe how suppressed exceptions prevent primary exceptions from being swallowed? If yes, you understand try-with-resources.

Use Cases

Managing file input and output streams safely without writing verbose finally cleanup blocks.

Guaranteeing database connection closure in transaction layers.

Common Mistakes

Declaring non-AutoCloseable classes in the try header, causing compilation errors.

Manually closing resources inside a finally block when using try-with-resources, duplicating operations.