Fundamentals 7 min read

Understanding Java try‑finally: Why the Method Returns "Cang Laoshi" Instead of "Xiao Ze"

This article explains the Java try‑finally mechanism, demonstrates with code why a method returns the original string "Cang Laoshi" rather than the updated value "Xiao Ze", and outlines when finally blocks are guaranteed to run or can be bypassed.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding Java try‑finally: Why the Method Returns "Cang Laoshi" Instead of "Xiao Ze"

Most companies include a written test in their interview process, and a poorly answered coding question can cause a candidate to be rejected, so understanding Java fundamentals is crucial.

Below is the interview code used to illustrate the behavior of try and finally :

public class TryFinallyDemo {
    public static void main(String[] args) {
        TryFinallyDemo tryFinallyDemo = new TryFinallyDemo();
        System.out.println(tryFinallyDemo.test());
    }

    public String test() {
        String name = "苍老师";
        try {
            return name;
        } finally {
            name = "小泽";
        }
    }
}

When this program runs, many expect the output to be "小泽" because the finally block changes the variable, but the actual output is "苍老师".

The reason lies in the Java exception‑handling contract: the finally block is always executed, even if a return statement appears in the try or catch blocks, but the value to be returned is determined before the finally block runs.

When a program opens resources such as database connections, network sockets, or file handles in the try block, those resources must be released in the finally block.

A complete Java exception‑handling skeleton looks like this:

public void test() {
    try {
        // business logic
    } catch (SubException e1) {
        // handle exception 1
    } catch (SubException e2) {
        // handle exception 2
    } finally {
        // resource cleanup
    }
}

The finally block runs regardless of whether an exception occurs, which catch block is executed, or whether a return statement is present.

The three possible execution paths are:

If no exception occurs, the try block finishes normally and then the finally block runs.

If an exception is caught, the corresponding catch block finishes and then the finally block runs.

If an exception is not caught, the finally block runs before the exception propagates outward.

Related interview question: Interviewer's view on Java exceptions .

Many people initially think the answer is "小泽", but running the code shows the output is actually "苍老师".

To understand why, we compile the class with javac and inspect the bytecode using javap . The bytecode reveals the following steps:

1. The string "苍老师" is pushed onto the operand stack and stored in local variable 1, then moved to local variable 2.

2. The string "小泽" is pushed onto the operand stack and stored back into local variable 1.

3. The value of local variable 2 (still "苍老师") is pushed onto the operand stack and returned.

Therefore, the method returns the original value.

If an exception occurs inside the try block, the finally block still executes before the exception propagates:

public String test() {
    String name = "苍老师";
    try {
        int a = 1/0;
        return name;
    } finally {
        System.out.println("finally");
        name = "小泽";
    }
}

Running this code prints "finally" and then throws an exception, confirming that finally runs even when an exception is thrown.

Note that if the try block calls System.exit(0) , the JVM terminates and the finally block will not be executed.

Summary

The article explains how the finally block works in Java, when it is guaranteed to execute, and the rare cases where it can be skipped, providing interview‑level insight for Java developers.

There is no technology that cannot be mastered; only a lack of willingness to learn.

BackendJavaexception handlingprogrammingInterviewtry-finally
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.