Sub topic 15
When close() Fails Too: Suppressed Exceptions
In the last chapter, you saw that try-with-resources closes resources in reverse order. Now comes a harder question. What if the body of the try block throws an exception, and then close() throws one too? Java has to report one of them. This chapter shows how Java keeps both.
The Problem: Two Failures at Once
Picture a program that writes to a file. The write fails, so an exception is thrown. Then Java closes the file, and the close fails too. You now have two exceptions.
The first one is the primary exception. It is the exception thrown by the body of the try block. It usually tells you the real cause of the trouble.
With old-style cleanup in a finally block, the exception from close() would replace the primary one. The real cause would be lost. You would see only "close failed" and never learn why the write failed.
The Answer: Suppressed Exceptions
A suppressed exception is an exception that Java attaches to another exception instead of throwing it. try-with-resources does this for you:
- The primary exception from the body is kept as the one that gets thrown.
- Any exception from
close()is added to the primary exception as a suppressed exception. - You can read the suppressed exceptions with the
getSuppressed()method. It returns an array ofThrowable.
A Worked Example
This program has a resource whose close() method always fails. The body of the try block fails as well.
public class SuppressedDemo {
static class Resource implements AutoCloseable {
private final String name;
Resource(String name) {
this.name = name;
}
@Override
public void close() {
System.out.println("closing " + name);
throw new IllegalStateException("close failed: " + name);
}
}
public static void main(String[] args) {
try (Resource a = new Resource("a"); Resource b = new Resource("b")) {
throw new RuntimeException("body failed");
} catch (RuntimeException e) {
System.out.println("caught: " + e.getMessage());
for (Throwable s : e.getSuppressed()) {
System.out.println("suppressed: " + s.getMessage());
}
}
}
}Running it prints:
closing b
closing a
caught: body failed
suppressed: close failed: b
suppressed: close failed: aHere is what happened:
- The body threw "body failed". This became the primary exception.
- Java closed
bfirst, because resources close in reverse order. Its failure was attached as suppressed. - Java still closed
a, even thoughbfailed. Its failure was attached too. - The
catchblock received "body failed", with both close failures listed inside it.
When Only close() Fails
If the body finishes normally and only close() fails, there is no primary exception to attach to. In that case, the exception from close() is thrown as normal, and it has no suppressed exceptions.
| What fails | Exception that is thrown | Suppressed list |
|---|---|---|
| Only the body | Body exception | Empty |
| Only close() | close() exception | Empty |
| Body and close() | Body exception | Holds the close() exceptions |
Exercise
In the example above, suppose only resource a's close() throws, and the body throws "body failed". What does getSuppressed() return, and which exception does the catch block receive?
Show answer
The catch block receives the primary exception, "body failed". getSuppressed() returns an array with one element, the IllegalStateException "close failed: a". Resource b closes without error, so nothing is added for it.
Exercise
Why is it better to keep the body's exception as the primary exception instead of the exception from close()?
Show answer
The body's exception usually describes the real cause of the problem. A close() failure often happens because of that first problem. If the close() exception replaced it, you would lose the information you need to find and fix the bug.