Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 11 / Topic 2 of 10

try-with-resources at scale, and suppressed exceptions

Part 7 opened one or two resources in a try-with-resources. Real code sometimes opens several, and real resources can fail to close cleanly (a half-written file, a network socket that won't flush). Two things to know once that happens:

  1. Close order is the reverse of open order. try (A a = ...; B b = ...) opens a then b, and closes b then a — last opened, first closed, the same rule as a finally nested inside another finally.
  2. If the try body throws, and then close() also throws, Java does not drop either exception. The body's exception becomes the one you catch; every exception thrown while closing is attached to it as a suppressed exception, retrievable with getSuppressed().

Think of returning two sets of borrowed keys to a watchman at the end of the day: if you trip and drop something (the body throws) and the watchman also can't find the right hook for one set of keys (close() throws), both problems are real — you don't get to pretend the second one didn't happen just because the first one already happened.

try (Ledger sales = new Ledger("sales.log", true);
     Ledger refunds = new Ledger("refunds.log", true)) {
    sales.write("Order 102: Rs 540");
    refunds.writeAndFail();                // throws first — this becomes the PRIMARY exception
} catch (IllegalStateException e) {
    System.out.println("Caught: " + e.getMessage());
    for (Throwable s : e.getSuppressed()) {
        System.out.println("  suppressed: " + s.getMessage());
    }
}
$ javac SuppressedExceptionDemo.java
$ java SuppressedExceptionDemo
-- two resources, both close cleanly --
sales.log <- Order 101: Rs 1200
refunds.log <- Refund 55: Rs 300
refunds.log: closing
sales.log: closing

-- the try body fails, and close() ALSO fails --
sales.log <- Order 102: Rs 540
refunds.log: closing
sales.log: closing
Caught: refunds.log: disk full while writing
Suppressed count: 2
  suppressed: refunds.log: failed to flush to disk on close
  suppressed: sales.log: failed to flush to disk on close

In the first block, refunds (opened second) closes before sales, and neither one throws. In the second block, refunds.writeAndFail() throws inside the body; Java still closes both resources (reverse order, same as always) before propagating anything, and since both close() calls also threw, both end up in getSuppressed() on the exception you actually catch — nothing silently disappears.

⚠️ Don't ignore suppressed exceptions by habit. A failed close() on a database connection or an output stream often means data didn't actually get written, even though your main logic "succeeded." e.getSuppressed() is exactly where that second failure is hiding.

Full program: examples/SuppressedExceptionDemo.java.

Done here? The practice set for Part 11 — including the solutions — is on the part page.

Search the guide