Today we are going to talk about the try catch finally
execution 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 try
execution return
is over, the result to be returned is stored in a temporary stack. The program will not return immediately, but will execute the finally
statement block. During execution value = 9
, the program will only overwrite the value
value, 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 value
will still be overwritten. When it is executed return value
, it will return directly value
and 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 catch
of the exception output "an exception occurred", the program will continue to execute fianlly
and then overwrite value
, and finally return the value
value.
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
catch
The return
result in will still be stored in a temporary stack, and finally
this value will be returned when the execution is complete.
Through the above example, we can draw conclusions:
-
Regardless of whether there is an exception, the
finally
code in the block will be executed; - When
try
andcatch
there isreturn
time,finally
still performs; finally
It isreturn
executed 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 isfinally
determined before execution;finally
It is best not to include itreturn
, otherwise the program will exit prematurely and the return value will no longer be the return value in thetry
ORcatch
block.
-------------------------------- END -------------------------------
For more exciting articles in time, please pay attention to the public account "Java Essentials".