JVM关键字try、catch、finally、return执行过程
2024-10-09 13:41:19
关键字:jvm try catch finally return、指令
- finally相当于在所有方法返回之前执行一次
- finally中含有return其中finally中return会覆盖try和catch中的return
- finally中不含return时,会先将try或catch中的返回值储存在局部变量表中,最后执行返回是加载到操作数栈返回
public class FinallyReturnBean { public int sayGoodBye(int divide){
int c = 0;
try {
c = 1000/divide;
return c;
}catch (Exception e){
c = 500;
return c;
}finally {
c =300;
// return c;//#1 注释return c; #2 不注释 return c;
}
} public static void main(String[] args) {
System.out.println(new FinallyReturnBean().sayGoodBye( 0));
System.out.println(new FinallyReturnBean().sayGoodBye( 1));
} // #1 注释return c; 500;1000
// public class com.lsl.common.jvm.bean.FinallyReturnBean {
// public com.lsl.common.jvm.bean.FinallyReturnBean();
// Code:
// 0: aload_0
// 1: invokespecial #1 // Method java/lang/Object."<init>":()V
// 4: return // public int sayGoodBye(int);
// Code:
// 0: iconst_0
// 1: istore_2
// 2: sipush 1000
// 5: iload_1
// 6: idiv
// 7: istore_2
// 8: iload_2
// 9: istore_3 --slot3
// 10: sipush 300
// 13: istore_2
// 14: iload_3 --slot3
// 15: ireturn --1000
// 16: astore_3
// 17: sipush 500
// 20: istore_2
// 21: iload_2
// 22: istore 4
// 24: sipush 300
// 27: istore_2
// 28: iload 4
// 30: ireturn --
// 31: astore 5
// 33: sipush 300
// 36: istore_2
// 37: aload 5
// 39: athrow
// Exception table:
// from to target type
// 2 10 16 Class java/lang/Exception
// 2 10 31 any
// 16 24 31 any
// 31 33 31 any
// } // #2 不注释 return c; 300;300 try return失效
// public class com.lsl.common.jvm.bean.FinallyReturnBean {
// public com.lsl.common.jvm.bean.FinallyReturnBean();
// Code:
// 0: aload_0
// 1: invokespecial #1 // Method java/lang/Object."<init>":()V
// 4: return
//
// public int sayGoodBye(int);
// Code:
// 0: iconst_0
// 1: istore_2
// 2: sipush 1000
// 5: iload_1
// 6: idiv
// 7: istore_2
// 8: iload_2
// 9: istore_3
// 10: sipush 300
// 13: istore_2
// 14: iload_2
// 15: ireturn --
// 16: astore_3
// 17: sipush 500
// 20: istore_2
// 21: iload_2
// 22: istore 4
// 24: sipush 300
// 27: istore_2
// 28: iload_2
// 29: ireturn --
// 30: astore 5
// 32: sipush 300
// 35: istore_2
// 36: iload_2
// 37: ireturn --
// Exception table:
// from to target type
// 2 10 16 Class java/lang/Exception
// 2 10 30 any
// 16 24 30 any
// 30 32 30 any
// } }
最新文章
- 设计模式C#实现(十六)——中介者模式
- socketserver服务器
- js和jquery页面初始化加载函数的方法及先后顺序
- qunit学习(一)
- Perl中的正则表达式
- Cheatsheet: 2013 07.01 ~ 07.08
- P1231: [Usaco2008 Nov]mixup2 混乱的奶牛
- codeforces 579D D. ";Or"; Game(前后缀+贪心)
- require.js入门指南(一)
- BZOJ2213: [Poi2011]Difference
- Struts 和Spring的核心控制器
- Effective Modern C++ Item 37:确保std::thread在销毁时是unjoinable的
- Android Paint类介绍以及浮雕和阴影效果的设置
- [CTSC2017]网络
- 关于Collections.sort()排序方法的源码探索
- pip问题
- opencv学习笔记(一)
- Win10系列:VC++调用自定义组件2
- MethodInfo类的一般使用
- keras中TimeDistributed