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
//    }

}
原文地址:https://www.cnblogs.com/qq610039525/p/12674266.html