This article brings you the content is about the Java finally and return execution order resolution (code example), there is a certain reference value, the need for friends can refer to, I hope to help you.
All know, finally the execution characteristics
1, regardless of the occurrence of wood anomalies, finally block code will be executed;
2. Finally will still execute when there is a return in the try and catch.
So the question is, what is the order of execution?
A simple test class and a post-compilation bytecode:
public class Test {
publicstatic void main(String[] args) {
System.out.println(test());
}
publicstatic int test() {
try{
System.out.println("Codesin try block.");
return0;
}catch (Exception e) {
System.out.println("Codesin catch block.");
return100;
}finally {
System.err.println("Codesin finally block.");
}
}
}
/*
public static int test();
Code:
0: getstatic #2 // Fieldjava/lang/System.out:Ljava/io/PrintStream;
3: ldc #5 // String Codes in try block.
5: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: iconst_0
9: istore_0
10: getstatic #7 // Field java/lang/System.err:Ljava/io/PrintStream;
13: ldc #8 // String Codes in finallyblock.
15: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
18: iload_0
19: ireturn
20: astore_0
21: getstatic #2 // Fieldjava/lang/System.out:Ljava/io/PrintStream;
24: ldc #10 // String Codes in catchblock.
26: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
29: bipush 100
31: istore_1
32: getstatic #7 // Fieldjava/lang/System.err:Ljava/io/PrintStream;
35: ldc #8 // String Codes in finallyblock.
37: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
40: iload_1
41: ireturn
42: astore_2
43: getstatic #7 // Fieldjava/lang/System.err:Ljava/io/PrintStream;
46: ldc #8 // String Codes in finallyblock.
48: invokevirtual #6 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
51: aload_2
52: athrow
*/
We can compare the code we write with the compiled bytecode:
Discovering that the virtual machine did a lot for us, it inserted the finally statement block between the Try,catch and the return statement. This is why the finally will be executed regardless of whether the exception is returned or not. Now that the finally is executed, return what is in the finally. Since finally all return is invalid, what is the meaning of return, so the actual finally block is not allowed to return. The eclipse compiler prompts Warning:finallyblock does not to complete normally.
Test code for several scenarios
try{return;} catch () {} finally{} return;
Test code:
1. Basic data types
Public static int test1(){
Int a = 10;
Try{
a = 20;
Return a;
}catch(Exception e){
//other codes.
}finally{
a += 5;
//other codes.
}
Return a;
}//The final return value is 20
2. Reference data type changes the value of the referenced object
Public static StringBuffer test2(){
StringBuffer sb = new StringBuffer("abc");
Try{
Return sb;
}catch(Exception e){
//other codes.
}finally{
Sb.append("DEF");
//other codes.
}
Return sb;
}//The final return content is abcDEF
Public static StringBuffer test3(){
StringBuffer sb = new StringBuffer("abc");
Try{
Return sb;
}catch(Exception e){
//other codes.
}finally{
Sb = new StringBuffer("DEF");
//other codes.
}
Return sb;
}//The final return value is abc
In this case, it can be found that, before finally, if there is a return statement, finally on the return variable no matter what changes, the variable itself will not be changed, such as for the basic type of variable re-assignment, or for the reference type of the variable to re-specify the reference is not recorded in the return value. But the statement in finally executes, and in finally, the object content to which the reference type's variable points is modifiable, and the modification is valid. This situation can be understood, encountered a return statement, the virtual machine to return the value of a set of houses, the house can be arbitrarily dismantled? No. But the people inside the house can change. The variables of the base data type and the reference type are the houses themselves, and the contents of the objects to which the reference type variable points are the people in the house.
There is such a small test:
public static int test(int num){
int a = 10;
try{
return a;
}catch(ArithmeticException e){
a = 20;
return a;
}finally{
a = 30;
return a;
}
}
What is the return value when there is no exception in the 1.1 try block?
What is the return value when there is a ArithmeticException exception in the 1.2 try block?
What is the return value when there are other exceptions in the 1.3 try block?
Answer: All 30. The reason is that after executing the code before return in the try block, the JVM inserts the code in the finally block between return and the finally block contains a return, then it returns directly.