Q23: What are Suppressed Exceptions in Java Try-With-Resources?
When an exception occurs inside a try block AND another exception occurs while automatically calling .close() during resource cleanup, the primary exception is thrown to the caller, while the cleanup exception is attached to it as a Suppressed Exception.
Retrieved using primaryException.getSuppressed(). Preventing cleanup failures from masking original root errors.
Key Takeaways
Preserves root exceptions while capturing secondary resource closing errors.
Accessed via
e.getSuppressed()array.
Knowledge Check
If both the try block and auto-close method throw an exception, which exception is thrown directly to the outer catch block?