zoukankan      html  css  js  c++  java
  • JVM关键字try、catch、finally、return执行过程

    关键字:jvm try catch finally return、指令

    1. finally相当于在所有方法返回之前执行一次
    2. finally中含有return其中finally中return会覆盖try和catch中的return
    3. 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
    //    }
    
    }
  • 相关阅读:
    关于callback
    vue项目前端限制页面长时间未操作超时退出到登录页
    vue 项目文件流数据格式转blob图片预览展示
    You are using the runtime-only build of Vue where the template compiler is not available.
    element-ui el-cascader级联选择器设置指定层级不能选中
    vue项目中图片预览旋转功能
    nhandled rejection Error: EPERM: operation not permitted, open 'C:Program Files odejs ode_cache npm ERR! cb() never called!
    vue+element-ui upload图片上传前大小超过4m,自动压缩到指定大小,长宽
    vue+element-ui 项目中实现复制文字链接功能
    vue项目js实现图片放大镜功能
  • 原文地址:https://www.cnblogs.com/qq610039525/p/12674266.html
Copyright © 2011-2022 走看看