Will the code in the finally block be called and run after a return statement is executed in the try block?
What if there is a return statement in the try block and the finally block as well?
What if exception is thrown in the try block and the return statement in finally block?

These are the common questions we get when we are using the finally block in exception handling. Let us see in detail about these flow of executions when we are using the return statements.

Question : Will the code in the finally block be called and run after a return statement is executed in the try block?

**Answer : **Yes the code in the finally block will be executed before the return statement is executed in try block.

**Tutorial : **

package com.javaindetail.finallyBlock;

public class ReturnInTryBlock {

  public static int callReturnInTry() {
    try {
      System.out.println("try block starts");
      return 1;
    } 
    finally {
      System.out.println("finally block is run before method returns.");
    }
  }

  public static void main(String[] args) {
    System.out.println(ReturnInTryBlock.callReturnInTry());
  }
}

Output :

try block starts
finally block is run before method returns.
1

**Question : **What if there is a return statement in the try block and the finally block as well?

**Answer : **In this scenario also finally gets executed. The return statement in the finally block overrides the return statement in the try block.

**Tutorial : **

package com.javaindetail.finallyBlock;

public class ReturnInTryAndFinally {

  public static int callReturnInTryAndFinally() {
    try {
      return 1;
    } 
    finally {
      return 2;
    }
  }

  public static void main(String[] args) {
    System.out.println(ReturnInTryAndFinally.callReturnInTryAndFinally());
  }

}

** Output : **

2

 

**Question : **What if exception is thrown in the try block and the return statement in finally block?

**Answer : **In this case finally return statement will override the exception that is thrown in the try block.

**Tutorial : **

package com.javaindetail.finallyBlock;

public class ReturnInTryAndFinally {

  public static int callExceInTryAndReturnFinally(){
    try{
        throw new NoSuchFieldException();
    } finally {
        return 1;
    }
}

  public static void main(String[] args) {
    System.out.println(ReturnInTryAndFinally.callExceInTryAndReturnFinally());
  }

}

**Output : **

1

Situations when finally will not run after the return statement

  1. If Systen.exit() is called first.
  2. If JVM – Java Virtual Machine crashes.

 

NOTE : A RETURN STATEMENT IN FINALLY BLOCK IS A BAD IDEA. IT IS NOT ENCOURAGED.