• 理清Java中try-catch-finally带return的执行顺序


    前言:try-catch-finally带return和异常时,它们之间执行顺序问题是留下来的一个小疑问,今天搞清楚它们

    第一种情况:无异常

        //1.try-catch-finally都带有return
        public static int method_1(){
            int num = 1;
            try {            
                System.out.println("---try run---");
                return ++num;
            } catch (Exception e) {
                System.out.println("---catch run---");
                return --num;
            }finally {
                num = 10;
                System.out.println("---finally run---");
                //return num; 
            }        
        }

    没有异常,程序执行完try代码块中的内容最后执行finally中的代码,输出结果为下:

    1.1:可能会有所疑惑:为什么在finally块中对num值进行了修改,输出的依然是2?  

    原因是:jdk走到try中的return时,将需要return的值放到了一个临时存储区中去,虽然finally中对num值进行了修改,并不会影响返回结果,返回的依然是临时存储区的那

    1.2: 当将finally代码块中的return注释去掉,返回值为什么为10?

     原因是:无论是在try或者是catch中进行了return,最后都会执行finally中的代码,如果在finally中进行了return,则函数到此就退出了。

    第二种情况:有异常

    public static int method_2(){
            int num = 1;
            try {        
                num = num/0;
                System.out.println("---try run---");
                return ++num;
            } catch (Exception e) {
                System.out.println("---catch run---");
                return --num;
            }finally {
                num = 10;
                System.out.println("---finally run---");
                //return num;
            }        
        }    

     输出结果如下:

    其实也很简单理解:执行try中的代码块到num=num/0,发现有错被catch捕捉到,执行catch中的代码块,最后在执行finally中的代码块,与第一种情况相同,catch中需要return的值被放到了栈中,所以finally中对num的改变并不会影响实际返回的值。

    小结:

    1.finally代码块中内容最后都会被执行,无论是在try中return还是catch中return

    2.finally中最好不要进行return操作,最好在try-catch中进行或者函数的末尾进行

    3.return的值会被存到临时存储区中去,即使finally代码块中对return值进行修改,也不会影响到最终返回值。

    练习题:

    练习题一:

    public static int method_3() {
            int num = 1; 
            try {
                num=1/0;
                return num;
            } catch (  Exception e) {
                System.out.println("catch run...");
                return num;
            } finally {
                ++num;
            }
         
        }

    练习题二:

    public static int method_4() {
            int num = 1;
            try {         
                return num;
            } catch (Exception e) {
                System.out.println("catch run...");        
            } finally {
                ++num;
            }
            return 20;
        }

    练习题三:

        public static int method_5() {
            int num = 1;
            try {
                num = 1 / 0;
                return num;
            } catch (ArithmeticException e) {
                System.out.println("catch_1 run ...");
                return --num;
            } catch (Exception e) {
                System.out.println("catch_2 run ...");
                return ++num;
            } finally {
                ++num;
            }
    
        }

    这题关键是清楚num=num/0报的是什么错误

     练习题五:

    public static ArrayList test(){
            ArrayList<String> list = new ArrayList<>();
            try{
                list.add("张三");
                System.out.println("try block");
                return  list;
            }catch (Exception e){
                list.add("李四");
                System.out.println("catch block");
                return  list;
            }finally {
                list.add("王五");
                System.out.println("finally block");
                return  list;
            }
        }

    运行完后list为["张三","王五"]。

    其他try-catch-finally练习题https://blog.csdn.net/jinzhu_li/article/details/38560203

    https://blog.csdn.net/qq_33275672/article/details/76585345?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase

  • 相关阅读:
    电脑内存九大常见问题解决方案
    学会快速装系统 图解硬盘分区软件Norton Ghost使用
    XP和Win 7双系统安装说明和注意事项
    Linux gzip、gunzip
    kylin的状态栏(启动器)改成ubuntu之前的样子
    chrome安装HTTP测试扩展
    程序员重要的事情
    PD003-NET通用后台系统
    通用后台管理系统(ExtJS 4.2 + Spring MVC 3.2 + Hibernate)
    UNICODE与ANSI的区别
  • 原文地址:https://www.cnblogs.com/zengcongcong/p/11315742.html
Copyright © 2020-2023  润新知