What problem does try-with-resources solve? What is a suppressed exception?
Short answer
try-with-resources calls close() for you when the block ends, whether it succeeded or failed. If both the body and close() fail, the original error is kept.
3 points to say:
- Resources close in reverse order of opening.
- The
close()error goes intogetSuppressed(). catchandfinallyrun after closing.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.