Will code in a Finally statement fire if I return a value in a Try block?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Yes. In languages such as Java, Python, and C#, code in a finally block runs even if the try block executes a return. The runtime prepares the return, executes the finally block on the way out, and only then completes the method exit, unless the finally block itself changes control flow by throwing or returning.
Why finally still runs
The purpose of finally is cleanup or guaranteed follow-up work. That guarantee would be useless if a return inside try could skip it.
The basic flow is:
- execute the
tryblock - encounter a
return - remember the return value or pending return action
- execute the
finallyblock - complete the return if
finallydoes not override it
That is why cleanup code such as closing files or releasing locks still runs.
Python example
Output:
The finally block runs before the function actually returns 10.
Java example
This behaves the same way: finally executes before the method returns its value.
The dangerous exception: finally can override control flow
The guarantee that finally runs does not mean the original return always survives. If the finally block itself returns a value or throws an exception, it can override what the try block was about to do.
Python example:
This returns 20, not 10.
That is exactly why returning from finally is usually considered a bad idea. It hides the earlier control flow and makes the function harder to reason about.
The same caution applies to exceptions
If the try block is about to return but the finally block throws an exception, the exception wins and the return never completes.
So the stronger rule is not just "finally runs." The stronger rule is "finally runs before the pending control flow is completed, and it can replace that control flow if it does something disruptive."
Use finally for cleanup, not for business logic
The cleanest use of finally is code that must happen no matter what:
- closing files
- releasing locks
- rolling back temporary state
- logging or metrics that must always run
The more you put ordinary decision logic or extra returns inside finally, the more surprising the code becomes.
Modern languages often offer better patterns too
Many languages now provide safer resource-management constructs such as:
- Python context managers with
with - Java try-with-resources
- C#
using
These patterns reduce the need to write manual cleanup in finally for common resource-lifetime problems.
Common Pitfalls
- Assuming
returninsidetryskips thefinallyblock. - Returning from the
finallyblock and silently overriding the original return value. - Throwing from
finallyand masking the earlier outcome fromtryorcatch. - Putting too much normal business logic into
finallyinstead of keeping it focused on cleanup. - Forgetting that modern resource-management constructs often express the intent more clearly than raw
tryandfinally.
Summary
- A
finallyblock runs even when thetryblock returns a value. - The runtime completes
finallybefore finishing the pending return. - If
finallyreturns or throws, it can override the original control flow. - Use
finallymainly for cleanup and guaranteed follow-up work. - Prefer higher-level resource-management constructs when the language provides them.

