Java Interview Questions on Exceptions and Resource Cleanup
Java exception questions often test which failure reaches the caller and whether resources are still closed. Try-with-resources gives resources a defined cleanup path. If cleanup also fails, the original body failure and a close failure are not simply interchangeable.
By Ravindra Wadile · Published

604 words · About 3 min read
Java exception questions often test which failure reaches the caller and whether resources are still closed. Try-with-resources gives resources a defined cleanup path. If cleanup also fails, the original body failure and a close failure are not simply interchangeable.
This Java 21 example uses a fictional local resource. It does not open files or delete data. Run the class with assertions enabled.
What happens when both work and close fail?
class Example {
static class SampleResource implements AutoCloseable {
@Override public void close() {
throw new IllegalStateException("close failed");
}
}
public static void main(String[] args) {
try (var resource = new SampleResource()) {
throw new IllegalArgumentException("work failed");
} catch (IllegalArgumentException error) {
assert "work failed".equals(error.getMessage());
assert error.getSuppressed().length == 1;
assert "close failed".equals(error.getSuppressed()[0].getMessage());
}
}
}
The body exception remains the primary failure in this case. The close failure is available as a suppressed exception. Preserving both helps explain what failed first without losing cleanup information.
Why not put every close call in a manual finally block?
A finally block can be correct, but manual cleanup must account for null resources, partial acquisition and failures during cleanup. Try-with-resources expresses an established ownership pattern for resources implementing the required interface.
It is not merely shorter syntax. The defined exception behavior is useful. A careless manual finally that throws can replace an earlier failure in a way the programmer did not intend.
In which order are several resources closed?
Resources declared together are closed in reverse order of initialization. This supports cases where a later resource wraps an earlier one. If creation of a later resource fails, already initialized resources still need cleanup under the construct's rules.
For an interview trace, name each acquired resource and mark whether its initialization succeeded before discussing closure. A resource that never finished being acquired cannot be treated like a fully constructed resource in the body.
Should a catch block always retry?
No. An exception type alone does not prove retrying is safe or useful. Understand whether the failed action had side effects, whether the underlying cause can change and whether the caller has a bounded retry policy.
A parse error in a fixed input is different from a temporary transport failure. Repeating an action indefinitely can turn a clear error into a hung request. Cleanup and retry policy are separate responsibilities.
When is wrapping an exception useful?
A higher layer may add useful context while retaining the original cause. Avoid wrapping only to replace a specific diagnostic with a vague message. Also keep internal diagnostic details out of public error responses when they contain sensitive implementation information.
The teaching example's messages are safe invented strings. Real paths, connection details and provider errors need a deliberate logging and response policy.
What is the difference between a suppressed exception and a cause?
A cause describes an underlying failure associated with another exception. A suppressed exception can record an additional failure that happened while another exception remained primary, as in this cleanup example. Inspect the appropriate relationship rather than assuming every secondary failure appears as getCause.
Useful practice checks
- The body fails and close succeeds.
- The body succeeds and close fails.
- Both fail, preserving the body failure and suppressed cleanup detail.
- Two resources record their actual close order.
For the next exercise, use a list to record close order instead of throwing from close. Then add the failures back one at a time. This makes it possible to explain lifetime and exception selection separately before combining them.
References and next practice
docs.oracle.com — java resources
java question collection · Browse interview topics · Practise a question in Interview Challenge · How to Answer Technical Interview Questions Out Loud