exception-handling
try-finally
code-execution
programming
return-statement

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:

  1. execute the try block
  2. encounter a return
  3. remember the return value or pending return action
  4. execute the finally block
  5. complete the return if finally does not override it

That is why cleanup code such as closing files or releasing locks still runs.

Python example

python
1def demo():
2    try:
3        print("try")
4        return 10
5    finally:
6        print("finally")
7
8print(demo())

Output:

text
try
finally
10

The finally block runs before the function actually returns 10.

Java example

java
1public class Demo {
2    static int value() {
3        try {
4            System.out.println("try");
5            return 10;
6        } finally {
7            System.out.println("finally");
8        }
9    }
10
11    public static void main(String[] args) {
12        System.out.println(value());
13    }
14}

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:

python
1def demo():
2    try:
3        return 10
4    finally:
5        return 20
6
7print(demo())

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 return inside try skips the finally block.
  • Returning from the finally block and silently overriding the original return value.
  • Throwing from finally and masking the earlier outcome from try or catch.
  • Putting too much normal business logic into finally instead of keeping it focused on cleanup.
  • Forgetting that modern resource-management constructs often express the intent more clearly than raw try and finally.

Summary

  • A finally block runs even when the try block returns a value.
  • The runtime completes finally before finishing the pending return.
  • If finally returns or throws, it can override the original control flow.
  • Use finally mainly for cleanup and guaranteed follow-up work.
  • Prefer higher-level resource-management constructs when the language provides them.

Course illustration
Course illustration

All Rights Reserved.