Environmental Science :HotSpot jdk8
>java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
java Source code
public class _01_ExceptionBytecode {
private int exec1(){
int i;
try {
i = exec2();
return i;
} catch (Exception e) {
e.printStackTrace();
} finally {
return -1;
}
}
private int exec2() throws Exception{
return 2;
}
}
Use javap Disassembly
javap -v -l -p _01_ExceptionBytecode.class
Unscramble the bytecode after de compilation
public org.byron4j.jvm._01_ExceptionBytecode();
// .... Omit some information about the class , Look directly at the method Code part
private int exec1();
Code:
0: aload_0 // aload_0: Push the first reference type local variable to the top of the stack
// because exec1 It's an instance method , Then the first local variable is this;
1: invokespecial #2 // invokespecial : Call superclass build method , Instance initialization method , Private method ; So this is calling theta exec2 Method
4: istore_1 // istore_1: Top the stack int Type 2 values are stored in the second local variable ; Here is i.
5: iload_1 // iload_1: Put the second int Push local variables to the top of the operand stack ; i = exec2()
6: istore_2 // istore_2: Top the stack int Type a values are stored in the third local variable ---- Here is the return value return i
7: iconst_m1 // Here is the beginning finally Block code : take int type -1 Push to top of stack ;
8: ireturn // Here is the beginning finally Block code : return -1;
9: astore_2 // Here is catch Code : Store the top reference value in the third local variable
10: aload_2 // Here is catch Code : Push the third reference type local variable to the top of the stack ; Exception e
11: invokevirtual #4 // Method java/lang/Exception.prin ; call e.printStackTrace();
tStackTrace:()V
14: iconst_m1 // Here is the beginning finally Block code : take int type -1 Push to top of stack ;
15: ireturn // Here is the beginning finally Block code : return -1;
16: astore_3 // Save results
17: iconst_m1 // ( real finally) Here is the beginning finally Block code : take int type -1 Push to top of stack ;
18: ireturn // ( real finally) Here is the beginning finally Block code : return -1;
Exception table: // Anomaly table
from to target type
0 7 9 Class java/lang/Exception // 0 To 7 Possible exceptions , Will be targeted 9 Capture java.langException type
0 7 16 any // 0 To 7 Possible exception , stay 16 May catch or throw any Types of abnormal ( error )
9 14 16 any // 9 To 14 Possible exception , stay 16 May catch or throw any Types of abnormal ( error )
// Stack frame information is omitted ...
private int exec2() throws java.lang.Exception;
Code:
0: iconst_2 // take int type 2 Push to top of stack
1: ireturn // Return from current method int ; return 2;
Exceptions:
throws java.lang.Exception
Let’s explain :
from JDK8 Start , Bytecode processing finally Time goes through redundancy finally Code block to solve .(jdk7 And before you can go through jsr Jump instruction handling .)
-
If the program runs normally :
- exec1 Will execute 0、1、4、5、6、7、8; there 7、8 It’s redundancy ( It can be thought that a copy of )finally Code block —— It’s about making sure that finally Requirements that must be implemented .
- In execution finally The code block is saved before try Return value of code block ; But in the end finally Of return Cover .
-
If there is a call exec2 There is Exception, Will perform 14、15、16, stay catch There will be redundancy in it finall The contents of the code block —— It’s also about making sure that finally Requirements that must be implemented .
summary : Through the understanding of the bytecode disassembled JVM Instructions , Can deepen right java Understanding of code execution process ; Anything else java The code can use the preceding javap Command view .
JVM Instruction set text
Here’s a share of JVM The Chinese version of the instruction set PDF
link :https://pan.baidu.com/s/1OAO7R7NRCqIFbkSCiOqaJQ
Extraction code :zimg