Suppression Order
What You'll Learn
You'll learn the exact sequence in which suppressed exceptions are recorded when multiple resources fail to close in try-with-resources.
The Order of Suppression
When try-with-resources closes multiple resources, it does so in reverse order of their declaration. If several close() calls throw exceptions, those exceptions become suppressed—but they're added to the array in the order the close() methods were attempted.
Think of it like stacking plates: you declare resources top-to-bottom, but close them bottom-to-top. If each plate breaks as you take it off, the cracks are recorded in the order you handled them.
How It Works
try (ResourceOne r1 = new ResourceOne();
ResourceTwo r2 = new ResourceTwo();
ResourceThree r3 = new ResourceThree()) {
// use resources
} catch (Exception e) {
// If body throws exception, it becomes primary
// If r3.close() throws, it's suppressed[0]
// If r2.close() throws, it's suppressed[1]
// If r1.close() throws, it's suppressed[2]
Throwable[] suppressed = e.getSuppressed();
}
Resources close in order: r3 → r2 → r1. Suppressed exceptions appear in getSuppressed() in that same chronological sequence—not in reverse, not in declaration order.
Why This Matters
When debugging, knowing that getSuppressed()[0] corresponds to the first resource that failed to close (the last declared) helps you trace the problem accurately. The array preserves the timeline of what went wrong.
Key Takeaway: Suppressed exceptions are added to the array in the order close() attempts occur—reverse declaration order—allowing you to reconstruct the exact sequence of cleanup failures.