Will Try / Finally without the Catch bubble the exception?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In modern programming, exception handling is a fundamental concept. It allows developers to manage runtime errors, ensuring programs can respond to unexpected conditions gracefully. A critical part of this error handling mechanism is the try
statement, frequently paired with catch
and finally
blocks. While the standard combination of try
, catch
, and finally
covers many scenarios, understanding the behavior of just try
and finally
—without catch
—is essential for developers who wish to ensure resource cleanup and still allow exceptions to bubble up.
Understanding try
/ finally
Without catch
In some cases, it is redundant or even undesirable to handle exceptions where they occur. Instead, you might want them to propagate to a higher level in the call stack. This is where a try
block combined with a finally
block, but without a catch
block, becomes handy.
Behavior Overview
When an exception is thrown, and there's no catch
block to intercept it, the finally
block still executes if it's present. This is crucial for scenarios where resources need to be released or certain cleanup actions must be performed, regardless of whether an error occurred.
Key Points:
- Exception Propagation: In the absence of a
catchblock, exceptions will propagate unchanged after thefinallyblock executes. - Resource Management: It's particularly useful for managing resources that must be released (e.g., file handles, network connections).
- Stack Unwinding: The exception continues with its stack unwinding process after executing the
finallyblock.
Example in Action
Let’s look at a practical example to illustrate how this works:
- The
tryblock attempts to divide a number by zero. - The
finallyblock ensures a cleanup message is printed, even though aZeroDivisionErroroccurs. - The exception is not caught within the
dividefunction, allowing it to propagate to where the function is called. - Exception Loss: Be cautious that you’re not masking higher-priority exceptions that could emerge from within
finallyitself. Iffinallythrows its own exception, the original exception may be lost. - Complexity in Debugging: Deliberately allowing exceptions to bubble can make debugging more complex, as the immediate source of an error might be less clear without deep stack traces.
Related reading
- wiremock issue when upgrading to Spring Boot 3
- With DynamoDb enhanced client from java aws sdk, how do you query using a compound keyConditionExpression?
- Working example of Spring Cloud Gateway with Redis session management?
- Would Java indexOf brute force method be more practical for me or some other substring algorithm?
- window.onbeforeunload not working on the iPad?
- Windows 10 pyinstaller tensorflow missing modules
- Write string to output stream
- Write to two Kafka topics in a single transaction using Spring Kafka

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.