💥 Exceptions & Errors · Intermediate

try-with-resources in Java

AutoCloseable resources closed automatically in reverse order.

🧩 The mysteryYou open a file, start reading, and an exception fires. The close() line below never runs. Do that 10,000 times on a server and the OS refuses to open any more files.

Auto-close

Declare a resource in parentheses after try and it is closed automatically when the block ends — normally or because of an exception. The resource type must implement **AutoCloseable** (Closeable extends it). Since Java 9, an effectively final variable can be listed directly: try (in) { ... }.

try (var in = Files.newBufferedReader(path)) {
    return in.readLine();
}   // in.close() is called automatically
🔮 Predict it

Two resources

What does this print?

record R(String n) implements AutoCloseable {
    public void close() { System.out.print(n); }
}
void main() {
    try (var a = new R("A"); var b = new R("B")) {
        System.out.print("body ");
    }
}
  1. body BA
  2. body AB
  3. BA body
Show the answer

Resources close after the body, in reverse order of declaration: last opened, first closed. Later resources often depend on earlier ones, so they must go first.

The order of events

When the body throws: 1. resources are closed (in reverse order), 2. the matching catch runs, 3. finally runs. So close() happens before your catch and finally blocks ever see the exception.

🤔 Think first

Close, catch, finally

A try-with-resources body throws IllegalStateException. There's a catch for it that prints X, a finally that prints F, and close() prints C. What's printed?

Think about it, then reveal the answer

CXF — the resource is closed as soon as the body exits, then the catch runs, and finally comes last.

⚠️ The trap

Not every object is a resource

Only AutoCloseable types can be declared in the parentheses. A plain class is a compile error — Java must know there's a close() to call.

class Plain { }
try (var p = new Plain()) {   // error
    System.out.println("ok");
}

Reading a file

✗ Leaks on error
var in = new FileInputStream(f);
byte[] data = in.readAllBytes();
in.close();

If readAllBytes() throws, close() never runs.

✓ Always closed
try (var in = new FileInputStream(f)) {
    byte[] data = in.readAllBytes();
}

Closed on every path, including exceptions.

💼 In the real world

Databases and pools

JDBC code opens a Connection, then a PreparedStatement, then a ResultSet — all in one try-with-resources, closed in reverse order. Leaking connections is a classic production outage: the pool runs dry and every request waits forever.

Key takeaways

  1. The resource type must implement AutoCloseable (Closeable extends it)
  2. Closed in reverse order: last opened, first closed
  3. close() runs before the catch and finally blocks
  4. Java 9+: an effectively final variable can be listed: try (in) { ... }
🤯 Did you know?

AutoCloseable was created in Java 7 as a new parent of the older Closeable (Java 5), so every existing stream worked with try-with-resources from day one.

Practice questions

What does this print?

record Res(String n) implements AutoCloseable {
    public void close() { System.out.println("close " + n); }
}
void main() {
    try (var a = new Res("A"); var b = new Res("B")) {
        System.out.println("body");
    }
}
  1. body close B close A
  2. body close A close B
  3. close A close B body
  4. body
Check your answer

body close B close A. Resources are closed after the body, in reverse order of declaration: B first, then A.

What does this print?

record Res() implements AutoCloseable {
    public void close() { System.out.print("C"); }
}
void main() {
    try (var r = new Res()) {
        throw new IllegalStateException();
    } catch (IllegalStateException e) {
        System.out.print("X");
    } finally { System.out.print("F"); }
}
  1. CXF
  2. XCF
  3. XFC
  4. CFX
Check your answer

CXF. The resource is closed as soon as the try body exits, before the catch runs; finally comes last.

Next: the body throws AND close() throws. Two exceptions, one catch block — which one survives?