Fundamentals 5 min read

What Happens to a finally Block When a try Returns in Java?

In Java, a finally block always executes before a method returns, even if the try block contains a return statement, and it can even override the original return value, though it may be skipped when the JVM terminates abruptly with System.exit.

ITPUB
ITPUB
ITPUB
What Happens to a finally Block When a try Returns in Java?

During a job interview, a candidate was asked what happens to the code inside a finally block when a return statement appears in the preceding try block. The common but incorrect belief is that the finally block would not run if no exception is thrown.

Basic try‑finally behavior

The following code demonstrates the actual behavior:

private static int testFinally() {
    try {
        System.out.println("Try!");
        return 1;
    } finally {
        System.out.println("Finally!");
    }
}

Running this method prints "Try!" followed by "Finally!" and then returns 1. The finally block executes before the method returns, regardless of whether an exception occurs.

Authoritative reference

Core Java, Volume I states: "Suppose you exit the middle of a try block with a return statement. Before the method returns, the finally block is executed. If the finally block also contains a return statement, then it masks the original return value." This confirms that finally always runs and can even override the original return.

When finally may not run

The only typical situation where a finally block is skipped is when the JVM terminates abruptly, for example via System.exit():

private static void testFinally() {
    try {
        System.out.println("Try!");
        System.exit(0);
    } finally {
        System.out.println("Finally!");
    }
}

Because System.exit(0) stops the virtual machine immediately, the finally block is never executed.

Practical resource‑closing pattern

A common use of finally is to close resources. One clear pattern separates the responsibilities of closing and exception handling:

InputStream in = ...;
try {
    try {
        // [1] Code may throw an exception.
    } finally {
        in.close(); // [2]
    }
} catch (IOException e) {
    // [3] Handle the exception.
}

Here the inner try‑finally solely ensures the stream is closed, while the outer try‑catch handles any IOException. However, if in.close() itself throws an exception, the outer catch will receive the closing‑exception ([2]) and the original exception ([1]) can be lost, which developers must watch out for.

Key takeaways

The finally block always runs before a method returns, even after a return in try.

A return inside finally overrides any earlier return value.

The only case where finally is skipped is when the JVM exits abruptly (e.g., System.exit).

When using finally for resource cleanup, be aware that exceptions thrown during cleanup can mask original exceptions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaException Handlingtry-finallyreturnsystem.exitresource-management
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

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.