1 2 3 4 5 6 7 8 9 10 |
public static void main(String[] args) { int i = 5; try{ i = 7; } catch (Throwable e) { }finally { i = 8; } } |
javap -c -p TestDynamicInvoke.class 反编译结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public static void main(java.lang.String[]); Code: 0: iconst_5 1: istore_1 2: bipush 7 4: istore_1 5: bipush 8 7: istore_1 8: goto 24 11: astore_2 12: bipush 8 14: istore_1 15: goto 24 18: astore_3 19: bipush 8 21: istore_1 22: aload_3 23: athrow 24: return Exception table: from to target type 2 5 11 Class java/lang/Throwable 2 5 18 any |
使用一种叫做冗余技术,如果没有发生异常,finally 块对应5,7行代码,如果2-5行之间发生被捕获异常那么从11行开始执行,如果是未被捕获的异常从18行开始执行, 可以看到finally块的字节码被冗余了3次,也就是总共会有2(正常流程+自动生成的catch any)+n(catch异常的次数)
Posted in: java基础
Comments are closed.