try { int a = 1; } catch (RuntimeException e) { int b = 2; } catch (Exception e) { int c = 3; }
stack=1, locals=3, args_size=1 0: iconst_1 1: istore_1 2: goto 14 5: astore_1 6: iconst_2 7: istore_2 8: goto 14 11: astore_1 12: iconst_3 13: istore_2 14: return Exception table: from to target type 0 2 5 Class java/lang/RuntimeException 0 2 11 Class java/lang/Exception
public class Foo { private int tryBlock; private int catchBlock; private int finallyBlock; private int methodExit; public void test() { try { tryBlock = 0; } catch (RuntimeException e) { catchBlock = 1; } finally { finallyBlock = 2; } methodExit = 3; } }
stack=2, locals=3, args_size=1 0: aload_0 1: iconst_0 2: putfield #2 // Field tryBlock:I 5: aload_0 6: iconst_2 7: putfield #3 // Field finallyBlock:I 10: goto 35 13: astore_1 14: aload_0 15: iconst_1 16: putfield #5 // Field catchBlock:I 19: aload_0 20: iconst_2 21: putfield #3 // Field finallyBlock:I 24: goto 35 27: astore_2 28: aload_0 29: iconst_2 30: putfield #3 // Field finallyBlock:I 33: aload_2 34: athrow 35: aload_0 36: iconst_3 37: putfield #6 // Field methodExit:I 40: return Exception table: from to target type 0 5 13 Class java/lang/RuntimeException # 监控try代码块 0 5 27 any # 监控try代码块 13 19 27 any # 监控catch代码块
@Data @AllArgsConstructor public class R implements AutoCloseable { private String name; @Override public void close() throws Exception { throw new RuntimeException(name); } public static void main(String[] args) throws Exception { try (R r1 = new R("R1"); R r2 = new R("R2")) { throw new RuntimeException("Init"); } } }
Exception in thread "main" java.lang.RuntimeException: Init at me.zhongmingmao.basic.exception.R.main(R.java:19) Suppressed: java.lang.RuntimeException: R2 at me.zhongmingmao.basic.exception.R.close(R.java:13) at me.zhongmingmao.basic.exception.R.main(R.java:20) Suppressed: java.lang.RuntimeException: R1 at me.zhongmingmao.basic.exception.R.close(R.java:13) at me.zhongmingmao.basic.exception.R.main(R.java:20)
103: astore 9 105: aload_2 106: aload 9 // 自动使用Supressed异常 108: invokevirtual #11 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 111: goto 118 114: aload_1 115: invokevirtual #10 // Method close:()V 118: aload 8 120: athrow
public class T { public void test() { try { int a = 1; } catch (E2 | E1 e) { int b = 2; } } } class E1 extends RuntimeException { } class E2 extends RuntimeException { }
stack=1, locals=3, args_size=1 0: iconst_1 1: istore_1 2: goto 8 5: astore_1 6: iconst_2 7: istore_2 8: return Exception table: // 按顺序生成多个异常表条目 from to target type 0 2 5 Class me/zhongmingmao/basic/exception/E2 0 2 5 Class me/zhongmingmao/basic/exception/E1
转载请注明出处:http://zhongmingmao.me/2018/12/19/jvm-basic-exception/
访问原文「JVM基础 -- 异常处理」获取最佳阅读体验并参与讨论