try catch finally 嵌套有关return

前言:
try{return;}中有return语句时,也有finally语句时,执行完finally后直接执行try中的return语句返回。不会再执行finally后的程序。如图所示:
在这里插入图片描述


有关嵌套try catch

在这里插入图片描述

public class Try {
    @Test
    public static int test1(){
        int a = 0;
        int b = 2;
        try {
            b = 0;
            a = 20;
            try {
                int c = a/b;
                System.out.println("内嵌try内:"+a+b+c);
                return a  ;
            }catch (Exception e){
                System.out.println("内嵌catch:"+a +"\t"+b);
            }finally{///
                b = 2;
                System.out.println("内嵌finally:"+a+"\t"+b);
            }
            System.out.println("内嵌try catch finally外:"+a+b);
            return a+b;
        }catch (Exception e){
            System.out.println("外部catch:"+a+b);
        }finally {
            System.out.println("外部finally:"+a+b);
            a = 30;
            System.out.println("外部finally:"+a+b);
            //return a;
        }
       return b;
    }
    public static void main(String[] args) {
        int c = test1();
        System.out.println("最终返回结果:"+c);
    }
}

运行结果是:

内嵌catch:20 0
内嵌finally:20 2
内嵌try catch finally外:202
外部finally:202
外部finally:302
最终返回结果:22 ///a+b的值

分析:内部try内有异常时:(1)此时内嵌的catch捕获异常后,外部的catch未执行
(2)内嵌的finally改变了基本变量int型b的值,等于2,
(3)此时执行的return语句是外部try内的语句,内嵌的try内的return语句未执行,返回的最终结果也是在内嵌finally内改变过的b的值。

===============================================================
如图:无异常

public static int test1(){
        int a = 0;
        int b = 2;
        try {
            b = 0;
            a = 20;
            try {
                //int c = a/b;
                System.out.println("内嵌try内:"+a+"\t"+b);
                return a  ;
            }catch (Exception e){
                System.out.println("内嵌catch:"+a +"\t"+b);
                b=5;
                //return a+b;
            }finally{
                b = 2;
                System.out.println("内嵌finally:"+a+"\t"+b);
            }
            System.out.println("内嵌try catch finally外:"+a+b);
            return a+b;
        }catch (Exception e){
            System.out.println("外部catch:"+a+b);
        }finally {
            System.out.println("外部finally:"+a+b);
            a = 30;
            System.out.println("外部finally:"+a+b);
            //return a;
        }
       return b;
    }
    public static void main(String[] args) {
        int c = test1();
        System.out.println("最终返回结果:"+c);
    }

结果是:

内嵌try内:200
内嵌finally:20 2
外部finally:202
外部finally:302
最终返回结果:20 //a的值

分析:内部try内无异常时:此时内嵌try内有return语句,内嵌finally后的语句不再执行(遵照前言中的标准),即最终执行的是内嵌try内的return语句返回a的值。

=========================================================

try {
            b = 0;
            a = 20;
            try {
                int c = a/b;
                System.out.println("内嵌try内:"+a+b+c);
                return a  ;
            }catch (Exception e){
                System.out.println("内嵌catch:"+a +"\t"+b);
                b=5;
                return a+b;
            }finally{
                b = 2;
                System.out.println("内嵌finally:"+a+"\t"+b);
            }
            //System.out.println("内嵌try catch finally外:"+a+b);
            //return a+b;
        }catch (Exception e){
            System.out.println("外部catch:"+a+b);
        }finally {
            System.out.println("外部finally:"+a+b);
            a = 30;
            System.out.println("外部finally:"+a+b);
            //return a;
        }
       return b;

执行结果是:

内嵌catch:20 0
内嵌finally:20 2
外部finally:202
外部finally:302
最终返回结果:25

分析:如图,(1)若内部catch捕获异常后内部catch内有return值,执行的是catch内的return,并且内部finally改变了b的值为2时,并未改变catch内的返回结果b的值是5。
(2)若倒数第二行return不注释的话输出的最终结果是30,即执行了外部try catch finally内的return。
(3)将内嵌catch内的return注释后,即此时所有try catch finally内都没有return语句,执行的是最外边的return语句,返回的最终结果是2。

=========================================================
第一幅图中的第三种情况操作实验,E处不会执行

public static int test1(){
        int a = 0;
        int b = 2;
        try {
            b = 0;
            a = 20;
            try {
                int c = a/b;
                System.out.println("内嵌try内:"+a+"\t"+b);
                //return a -5 ;
            }/*catch (Exception e){
                System.out.println("内嵌catch:"+a +"\t"+b);
                b=5;
                //return a+b;
            }*/finally{
                b = 2;
                System.out.println("内嵌finally:"+a+"\t"+b);
            }
            System.out.println("内嵌try catch finally外:"+a+b);// E处
            //int c = a/b;
            return a+b;
        }catch (Exception e){
            System.out.println("外部catch:"+a+b);
        }finally {
            System.out.println("外部finally:"+a+b);
            a = 30;
            System.out.println("外部finally:"+a+b);
            //return a;
        }
       return b;
    }
    public static void main(String[] args) {
        int c = test1();
        System.out.println("最终返回结果:"+c);
    }

执行结果是:

内嵌finally:20 2
外部catch:202
外部finally:202
外部finally:302
最终返回结果:2

更详细有关return的详解见另一篇博客:https://blog.csdn.net/weixin_41024458/article/details/85160027

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值