Understanding the Execution Order of finally and return in Java

This article explains how Java's finally block always runs before a method returns, demonstrates the effect of return statements placed before, inside, or after finally with code examples, and clarifies the differences among final, finally, and finalize.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding the Execution Order of finally and return in Java

Interviewers often ask which executes first, finally or return, in Java. This article explains the execution order and the purpose of finally. finally is intended for cleanup actions that must run after try/catch, such as releasing database or Redis connections, and it is always executed before the method returns.

Example 1 shows a return inside try followed by a finally block without its own return. The output demonstrates that the return value is produced after the finally block runs.

package com.test;

/**
 * @author Damon
 * @date 2020年3月18日 上午11:02:08
 */
public class App {
  public static void main(String[] args) {
    System.out.println("return result: " + test());
  }

  public static int test() {
    try {
      Thread.sleep(1);
      System.out.println("執行 return 1");
      return 1; // return in try, finally runs afterwards
    } catch (InterruptedException e) {
      e.printStackTrace();
      return -1;
    } finally {
      System.out.println("执行 finally");
      //return 3;
    }
    //System.out.println("執行 return 2");
    //return 1;
  }
}

結果:
執行 return 1
执行 finally
return result: 1

Example 2 places a return 3 inside the finally block. In this case the finally return overrides the earlier return, and the method finally returns 3.

package com.test;

/**
 * @author Damon
 * @date 2020年3月18日 上午11:02:08
 */
public class App {
  public static void main(String[] args) {
    System.out.println("return result: " + test());
  }

  public static int test() {
    try {
      Thread.sleep(1);
      System.out.println("執行 return 1");
      return 1; // return in try, finally runs afterwards
    } catch (InterruptedException e) {
      e.printStackTrace();
      return -1;
    } finally {
      System.out.println("执行 finally");
      return 3;
    }
    //System.out.println("執行 return 2");
    //return 1;
  }
}

結果:
執行 return 1
执行 finally
return result: 3

Example 3 puts the return after the finally block. The finally still runs first, then the subsequent statements execute and the method returns the value from the later return.

package com.test;

/**
 * @author Damon
 * @date 2020年3月18日 上午11:02:08
 */
public class App {
  public static void main(String[] args) {
    System.out.println("return result: " + test());
  }

  public static int test() {
    try {
      Thread.sleep(1);
      //System.out.println("執行 return 1");
      //return 1;
    } catch (InterruptedException e) {
      e.printStackTrace();
      //return -1;
    } finally {
      System.out.println("执行 finally");
      //return 3;
    }
    System.out.println("執行 return 2");
    return 1;
  }
}

結果:
执行 finally
執行 return 2
return result: 1

The conclusions are: when a return appears before finally, the finally block runs first and then the return value is applied; if finally itself contains a return, that value is returned immediately, bypassing any previous return; and a return after finally is executed after the cleanup code.

The article also briefly distinguishes final, finally, and finalize: final declares immutable variables, non‑overridable methods, or non‑extendable classes; finally is a block that always executes in exception handling; finalize is a method of Object invoked by the garbage collector for resource cleanup.

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 HandlingCode Examplefinallyreturn
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

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.