Exception table
The catch exception in each try statement block corresponds to one of the exception tables. Each of the exception tables includes:
Start Point
End Point, always increase the maximum pc pointer offset of the catch exception position by 1
Pc pointer offset in the bytecode sequence to jump to when handling exceptions
Constant pool index of caught exception classes
For example:
Public class Test {
Public static void main (String [] args ){
Try {
Class. forName ("java. lang. String ");
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
}
}
Use javap-c to view the bytecode as follows:
Compiled from "Test. java"
Public class Test extends java. lang. Object {
Public Test ();
Code:
0: aload_0
1: invokespecial #1; // Method java/lang/Object. "<init>" :() V
4: return
Public static void main (java. lang. String []);
Code:
0: ldc #2; // String java. lang. String
2: invokestatic #3; // Method java/lang/Class. forName :( Ljava/lang/String;) Ljava/lang/Class;
5: pop
6: goto 14
9: astore_1
10: aload_1
11: invokevirtual #5; // Method java/lang/ClassNotFoundException. printStackTrace :() V
14: return
Exception table:
From to target type
0 6 9 Class java/lang/ClassNotFoundException
}
It can be seen that the ClassNotFoundException exception may be between 0 and ~ Throw between 6 and the code at the beginning of 9 handles this exception.
When an exception occurs, jvm searches for the matched items in the entire exception table. If the current pc is within the range specified by the exception table entry, in addition, if the thrown exception is the class or its subclass pointed to by this entry, it jumps to the corresponding processing code to continue execution.
Methods may throw checked exceptions
The attribute_info in the Class file contains the Exceptions attribute, which records the exception information of each method throws. For details, refer to the articles related to the class file format.
The athrow command pops up the Throwable object reference from the top of the stack and throws an exception.
Finally statement
In the jvm specification, finally statements are implemented through jsr/jsr_w and ret commands. When jsr/jsr_w is executed, the return address after finally execution is completed is pushed into the stack. When finally is entered, the address is immediately saved to a local variable. After the execution is completed, ret extracts the return address from this local variable .??? Why is the return address saved to a local variable first ??? Because, when the return address from the finally statement needs to pop up in the stack, when the finally statement ends abnormally (break, continue, return, throwing an exception) you do not need to consider this issue.
The following section describes Compiling finally in the jvm specification:
Void tryFinally (){
Try {
TryItOut ();
} Finally {
WrapItUp ();
}
}
The compiled code is
Method void tryFinally ()
0 aload_0 // Beginning of try block
1 invokevirtual #6 // Method Example. tryItOut () V
4 jsr 14 // Call finally block
7 return // End of try block
8 astore_1 // Beginning of handler for any throw
9 jsr 14 // Call finally block
12 aload_1 // Push thrown value
13 athrow // and rethrow the value to the invoker
14 astore_2 // Beginning of finally block
15 aload_0 // Push this
16 invokevirtual #5 // Method Example. wrapItUp () V
19 ret 2 // Return from finally block
Exception table:
From To Target Type
0 4 8 any
After tryItOut is excluded from any exception, it will be captured by any item in the exception table. After finally is executed, the athrow command will be executed to throw the exception.
From a jdk version, the bytecode containing jsr, jsr_w, and ret will not be compiled. Due to instruction defects, the jvm inspection and analysis system has a vulnerability.
Besides, the finally exited abnormally.
Using break, continue, return, and throw exceptions in finally is considered as an abnormal finally end. When the ret command ends abnormally, the ret command will not be executed and unexpected results may occur. For example:
Public class Test {
Public static boolean test (boolean B ){
While (B ){
Try {
Return true;
} Finally {
/**//*
Break; always returns false
Continue; an endless loop occurs when javac is compiled and executed in java.
In eclipse, an error even occurs: The system prompts that the main class is not found.
Return false; always return false
Throw new RuntimeException (""); throws an exception
*/
}
}
Return false;
}
Public static void main (String [] args ){
& Nb