代码片段1
public class TestMain {
public static void main(String[] args) {
try {
String A="hello world !";
}catch (Exception e){
e.printStackTrace ();
}
finally {
System.out.println ( "nihao" );
}
}
}
Compiled from "TestMain.java"
public class TestMain {
public TestMain();
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 hello world !
2: astore_1
3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
6: ldc #4 // String nihao
8: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
11: goto 41
14: astore_1
15: aload_1
16: invokevirtual #7 // Method java/lang/Exception.printStackTrace:()V
19: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
22: ldc #4 // String nihao
24: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
27: goto 41
30: astore_2
31: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
34: ldc #4 // String nihao
36: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
39: aload_2
40: athrow
41: return
Exception table:// 异常表
from to target type
0 3 14 Class java/lang/Exception//0-3号指令中,碰到 Exception 时,跳到 14 号指令
0 3 30 any
14 19 30 any
}
异常表
属性表(attribute_info)可以存在于 Class 文件、字段表、方法表中,用于描述某些场景的专有信息。属性表中有个 Code 属性,该属性在方法表中使用,Java 程序方法体中的代码被编译成的字节码指令存储在 Code 属性中。而异常表(exception_table)则是存储在 Code 属性表中的一个结构,这个结构是可选的。
异常表结构
异常表结构如下表所示。它包含四个字段:如果当字节码在第 start_pc 行到 end_pc 行之间(即[start_pc, end_pc))出现了类型为 catch_type 或者其子类的异常(catch_type 为指向一个 CONSTANT_Class_info 型常量的索引),则跳转到第 handler_pc 行执行。如果 catch_type 为0,表示任意异常情况都需要转到 handler_pc 处进行处理。
类型 | 名称 | 数量 |
---|---|---|
u2 | start_pc | 1 |
u2 | end_pc | 1 |
u2 | handler_pc | 1 |
u2 | catch_type | 1 |