try catch fianlly execution order

Today we are going to talk about the try catch finallyexecution order. Under normal circumstances, it is easy to distinguish. In actual work, there will be no very complicated situations, but sometimes in the interview, in order to test the basic skills of the interviewer, some very complicated are specially designed. Case. This article will give you a summary, their execution order, I hope to help you.

Consider the following example:

public class TryCatchFinallyExecutionOrderTest {

  @Test
  public void testGetValue1() {
    System.out.println(getValue1());
  }

  public int getValue1() {
    int value = 3;
    try {
      value = 6;
      return value;
    } finally {
      value = 9;
    }
  }
}

Output: 6

When the tryexecution returnis over, the result to be returned is stored in a temporary stack. The program will not return immediately, but will execute the finallystatement block. During execution value = 9, the program will only overwrite the valuevalue, but will not update it. After the value in this temporary stack is executed, it will notify the main program that it finally{}has been executed and can request a return, and the value of the temporary stack will be taken out and returned.

Next, we modify the above example:

package com.nightsoul.imaged.mp.backend;

import org.junit.Test;

public class TryCatchFinallyExecutionOrderTest {

  @Test
  public void testGetValue2() {
    System.out.println(getValue2());
  }

  public int getValue2() {
    int value = 3;
    try {
      value = 6;
      return value;
    } finally {
      value = 9;
      return value;
    }
  }
}

Output: 9

The process finally{}before the program is executed is the same as the previous code, but when it is executed finally{}, it valuewill still be overwritten. When it is executed return value, it will return directly valueand end the program.

Let me change the example again:

public class TryCatchFinallyExecutionOrderTest {

  @Test
  public void testGetValue3() {
    System.out.println(getValue3());
  }

  public int getValue3() {
    int value = 3;
    try {
      value = 1 / 0;
      return value;
    } catch (Exception e) {
      System.out.println("Error occurs");
      return value;
    } finally {
      value = 9;
      return value;
    }
  }
}

Output: 9

An exception occurred in this program. After the execution catchof the exception output "an exception occurred", the program will continue to execute fianllyand then overwrite value, and finally return the valuevalue.

Modify the example again:

public class TryCatchFinallyExecutionOrderTest {

  @Test
  public void testGetValue4() {
    System.out.println(getValue4());
  }

  public int getValue4() {
    int value = 3;
    try {
      value = 1 / 0;
      return value;
    } catch (Exception e) {
      System.out.println("Error occurs");
      return value;
    } finally {
      value = 9;
    }
  }
}

Output: 3

catchThe returnresult in will still be stored in a temporary stack, and finallythis value will be returned when the execution is complete.

Through the above example, we can draw conclusions:

  1. Regardless of whether there is an exception, the finallycode in the block will be executed;

  2. When tryand catchthere is returntime, finallystill performs;
  3. finallyIt is returnexecuted after the calculation of the following expression (the value after the operation is not returned at this time, but the value to be returned is saved first, regardless of the code in the finally, the returned value will not change, still Is the value saved before), so the return value of the method is finallydetermined before execution;
  4. finallyIt is best not to include it return, otherwise the program will exit prematurely and the return value will no longer be the return value in the tryOR catchblock.

-------------------------------- END -------------------------------

For more exciting articles in time, please pay attention to the public account "Java Essentials".

Guess you like

Origin blog.51cto.com/xtayfjpk/2675482