• try、catch、finally带return的执行顺序总结


    如果try中没有异常,则顺序为try→finally,如果try中有异常,则顺序为try→catch→finally,但是当try、catch、finally中加入return之后,就会有几种不同的情况出现:

    一:try中带有return

      1、基本类型的情况下:

      

     1 public static void main(String[] args) {
     2         int i = testCep1();
     3         System.out.println(i);
     4     }
     5 
     6     private static int testCep1() {
     7         int i = 1;
     8         try {
     9              i++;
    10              System.out.println("try:" + i);
    11              return i;
    12          } catch (Exception e) {
    13              i++;
    14              System.out.println("catch:" + i);
    15          } finally {
    16              i++;
    17              System.out.println("finally:" + i);
    18          }
    19          return i;
    20      }

    输出:

      

       当try中带有return时,会先执行return前的代码,然后暂时保存需要return的信息,再执行finally中的代码,最后再通过return返回之前保存的信息,因此:try中计算后的2,而非finally中计算后的3

      2、引用类型的情况下:

      

    public static void main(String[] args) {
            List<Integer> list = testCep2();
            System.out.println(list);
        }
    
        private static List<Integer> testCep2() {
            List<Integer> list = new ArrayList<>();
            try {
                list.add(1);
                System.out.println("try:" + list);
                return list;
            } catch (Exception e) {
                list.add(2);
                System.out.println("catch:" + list);
            } finally {
                list.add(3);
                System.out.println("finally:" + list);
            }
            return list;
        }

    输出:

      

       引用类型保存的是变量的地址,所以变量的值在finally中发生改变后,会改变

    二:catch中带有return

     1 public static void main(String[] args) {
     2         int i = testCep3();
     3         System.out.println(i);
     4     }
     5 
     6     private static int testCep3() {
     7         int i = 1;
     8         try {
     9             i++;
    10             System.out.println("try:" + i);
    11             int x = i / 0 ;
    12         } catch (Exception e) {
    13             i++;
    14             System.out.println("catch:" + i);
    15             return i;
    16         } finally {
    17             i++;
    18             System.out.println("finally:" + i);
    19         }
    20         return i;
    21     }

    输出:

      

      catch中return值会覆盖try中的retrun值, finally中涉及到变量如果是基本类型不影响,如果是引用类型会影响

    三:finally中带有return

    public static void main(String[] args) {
            int i = testCep4();
            System.out.println(i);
        }
    
        private static int testCep4() {
            int i = 1;
            try {
                i++;
                System.out.println("try:" + i);
                int x = i / 0 ;
                return i;
            } catch (Exception e) {
                i++;
                System.out.println("catch:" + i);
                return i;
            } finally {
                i++;
                System.out.println("finally:" + i);
                return i;
            }
        }

    输出:

      

     当finally中有return的时候,try中和catch中的return会失效,在执行完finally的return之后,就不会再执行try中的return,一旦finally里出现异常,会导致catch中的异常被覆盖

  • 相关阅读:
    使用mybatis如果类属性名和数据库中的属性名不一样取值就会为null
    学习mybatis时出现了java.io.IOException: Could not find resource EmployeeMapper.xml
    配置mybatis-config.xml出现过很诡异的现象
    mybatis学习(一)
    报错cannot be cast to javassist.util.proxy.Proxy
    列车调度
    三角形
    土豪聪要请客(stol)
    Jams倒酒(pour)
    Data
  • 原文地址:https://www.cnblogs.com/xl118/p/13203544.html
Copyright © 2020-2023  润新知