原文地址:
https://my.oschina.net/bieber/blog/703251
一、单层的try/catch
public int test(int a,int b){ try{ return a+b; }catch (Exception e){ throw new CustomException(); } }
通过javap -v查看JVM编译成class字节码之后是如何处理这个try/catch的
public int test(int, int); flags: ACC_PUBLIC Code: stack=2, locals=4, args_size=3 0: iload_1 // 将第一个int参数压入队列(第一个入参) 1: iload_2 // 将第二个int参数压入队列(第二个入参) 2: iadd //弹出队列中第一个和第二个参数执行相加,并把相加结果压入队列 3: ireturn //弹出队列第一个元素,并return。 4: astore_3 //此处是try开始的逻辑 5: new #3 // class com/bieber/demo/CustomException 8: dup 9: invokespecial #4 // Method com/bieber/demo/CustomException."<init>":()V 12: athrow //将队列中的第一个元素弹出,并当做异常抛出,到此整个方法体完毕 Exception table: from to target type 0 3 4 Class java/lang/Exception LineNumberTable: line 13: 0 line 14: 4 line 15: 5 LocalVariableTable: Start Length Slot Name Signature 5 8 3 e Ljava/lang/Exception; 0 13 0 this Lcom/cainiao/cilogisticservice/ExceptionClass; 0 13 1 a I 0 13 2 b I StackMapTable: number_of_entries = 1 frame_type = 68 /* same_locals_1_stack_item */ stack = [ class java/lang/Exception ]