Can a finally block cause unexpected behaviour or side effects?

While a finally block is designed to run reliably regardless of how the try block exits, it can cause unexpected behavior when it throws its own exception or returns a value, both of which silently override whatever happened in the try block.

Key Points

Example

A method that throws a custom exception in its try block but then fails to close a stream in its finally block will surface only the IOException from the close, completely hiding the original, likely more important, exception.

try {
    return riskyOperation();
} finally {
    stream.close(); // if this throws, it replaces any exception above
}

Interview Tip

A concise interview answer is

Yes — a finally block that throws or returns can silently discard the original exception from the try block, which is a subtle source of lost error information. I avoid that by keeping finally blocks minimal and using try-with-resources, which attaches cleanup failures as suppressed exceptions instead of overwriting the original one.

Last updated: 11 November 2026

Previous question: When have you used a finally block? Describe a scenario.

Next question: What is a deadlock in multithreading? How can you prevent it?

More Exception Handling interview questions

All Exception Handling questions