Quick revision
The short answer
It prints "body 1". The exception thrown in the try body is the one that propagates, and the IllegalStateException thrown by close() is attached to it as a suppressed exception, so e.getSuppressed() has one element. Before Java 7 there was no such mechanism: with try/finally, an exception from close() replaced the body's exception, and the only way to see both was to catch the close exception yourself and log it.
What to remember
In this answer, we saw that the snippet prints "body 1": try-with-resources keeps the body's exception as the primary one and attaches the exception from close() to it as suppressed, readable with getSuppressed()[0]. Before Java 7, a finally block that threw replaced the original exception, so the real cause was lost unless we caught the close exception ourselves. We can use Throwable.addSuppressed in our own cleanup code, and stack traces show suppressed exceptions under a Suppressed: line.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
It prints "body 1". The exception thrown in the try body is the one that propagates, and the IllegalStateException thrown by close() is attached to it as a suppressed exception, so e.getSuppressed() has one element. Before Java 7 there was no such mechanism: with try/finally, an exception from close() replaced the body's exception, and the only way to see both was to catch the close exception yourself and log it.
Why this question is asked
Almost every resource we open, a file, a JDBC connection, a network stream, has to be closed, and closing can fail too. When both the work and the cleanup fail, we have two exceptions and only one can be thrown. Which one wins decides whether the log shows the real cause or a misleading cleanup error. Interviewers ask this to see whether we know how try-with-resources settles that conflict and what it replaced. In this answer, we will recap how try-with-resources works, trace the snippet step by step, look at the same code written the old way to see how the real cause was lost, and then see how to read suppressed exceptions in stack traces and how to add them by hand in our own cleanup code.
1A Quick Recap of try-with-resources
try-with-resources is the form of try that declares resources in parentheses, as in try (R r = new R()). Any object that implements AutoCloseable can be declared there. When the block finishes, whether it completes normally or throws, Java calls close() on each resource automatically, in the reverse order of declaration.
The interesting case is when two things go wrong. The body throws an exception, and then close() throws another one while the first is still on its way out. Java 7 added the idea of a suppressed exception to handle this: the body's exception stays the primary exception that propagates, and the exception from close() is recorded inside it, in a list that we read with getSuppressed(). Nothing is thrown away.
2Tracing the Snippet Line by Line
Let us run the code from the question and follow what happens at each step.
6 messages in order. Play them, or step through them one at a time.
The body's exception wins and carries the close exception with it.
The body throws RuntimeException("body"). Before control can reach the catch block, the resource must be closed, so Java calls r.close(). That call throws IllegalStateException("close"). Java does not let the second exception replace the first. It calls primary.addSuppressed(closeException) and then continues propagating the primary one.
Notice that the catch block belongs to the same statement, but it runs after the resources are closed. So by the time e arrives in the catch, the suppressed exception is already attached. e.getMessage() is "body", and e.getSuppressed() is an array with one element, the IllegalStateException. That is why the output is "body 1".
3The Same Code Before Java 7
Before Java 7 we closed resources in a finally block. Here is the equivalent code written that way, which is still common in old codebases.
The rule in Java is that if a finally block throws, its exception replaces whatever was propagating. So the caller sees the close failure, which is usually a symptom, and the real cause, the body's failure, disappears completely. In a production incident this is painful: the log says a stream failed to close, while the actual problem was, say, a parse error a few lines earlier.
To see the lost exception before Java 7, we had to catch the close exception inside finally and decide what to do with it ourselves. The usual pattern remembered whether the body had failed, and if it had, logged the close exception and swallowed it so the primary one could propagate. Libraries such as Apache Commons IO offered closeQuietly helpers for this, which closed the resource and ignored or logged any exception.
4Seeing Both Exceptions Without try-with-resources
The same idea can be written by hand. We keep the primary exception in a variable and, if close() fails too, attach the close exception to it with Throwable.addSuppressed. This method is public since Java 7, so we can use it in any cleanup code that try-with-resources cannot express, such as closing several things in a loop.
This is, in essence, what the compiler generates for try-with-resources. Writing it by hand is rarely needed, but it is useful in our own frameworks, for example when a shutdown method closes ten components and we want to report every failure, not just the first.
5Reading Suppressed Exceptions in a Stack Trace
When the primary exception is printed with printStackTrace() or passed to a logger, the suppressed exceptions appear under a line that starts with Suppressed:, indented beneath the primary trace.
So in a log, the first line tells us the real failure, and the Suppressed: block tells us that the cleanup also failed. If our log output never shows Suppressed: lines, it is worth checking that we log the exception object and not just its message, because the message alone carries neither the cause nor the suppressed list.
6Follow-up questions
What if the body succeeds and only close() throws?
Then there is no primary exception, so the exception from close() is thrown normally and the catch block receives it. Its getSuppressed() array is empty. Suppression only happens when an exception is already propagating.
What happens with two resources whose close() both throw?
Resources are closed in reverse order of declaration. If the body threw, both close exceptions are added to the body's exception as suppressed, in the order they happened. If the body did not throw, the first close exception becomes the primary one and the second is added to it as suppressed.
Can suppression be turned off?
Yes, for our own exception classes. Throwable has a protected constructor with an enableSuppression flag. If we pass false, addSuppressed does nothing and getSuppressed returns an empty array. The JVM uses this for some preallocated exceptions, but in application code we almost never want it.
7Conclusion
In this answer, we saw that the snippet prints "body 1": try-with-resources keeps the body's exception as the primary one and attaches the exception from close() to it as suppressed, readable with getSuppressed()[0]. Before Java 7, a finally block that threw replaced the original exception, so the real cause was lost unless we caught the close exception ourselves. We can use Throwable.addSuppressed in our own cleanup code, and stack traces show suppressed exceptions under a Suppressed: line.
Open with
It prints body 1. In try-with-resources, the body throws first, then Java closes the resource before the catch runs. close throws IllegalStateException, and instead of letting it replace the body's exception, the generated code calls addSuppressed on the primary exception.
Then, if they want more
So the catch receives the RuntimeException with message body, and getSuppressed has one element, the IllegalStateException from close. Before Java 7 we wrote the same thing with try and finally, and the rule there is that an exception thrown from finally replaces the one in flight.
So the caller would see only the close failure and the real cause would be lost. To see it, we had to catch the close exception inside finally, log it, and let the original propagate, or use helpers like closeQuietly. Today, if I write cleanup code that try-with-resources cannot express, I use Throwable.addSuppressed myself so no failure is lost.
And when I read logs, suppressed exceptions show up under a Suppressed: line below the main stack trace, which only appears if we log the exception object rather than its message.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 7
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does this print, and how would you see the lost exception before Java 7?": https://scrivio-interview-library.netlify.app/java/exceptions/7dc63341d9/