try catch 中带返回值执行顺序

    一段代码如果可能抛出异常,我们通常就会用try catch代码块包起来,以防止当程序crash掉,增强代码的健壮性。如果程序在执行这段代码后有些操作必须得完成,比如IO流用完后得关闭,就得用到finally。正常的try catch finally语句没有太多要讲的,也很容易理解,但是如果try catch中遇到return 语句就比较复杂了。看下面这个例子
例1:
     public class TrycatchTest {
    public static void main(String[] args) {
        System.out.println("x:" + new TrycatchTest().test());
    }

    int test() {
        int x = 1;
        try {
            System.out.println("try start");
            x++;
            return x;
        } finally {
            System.out.println("finally start");
            x++;
            System.out.println("finally end");
        }
    }
}
输出的x是2还是3?
结果:
try start
finally start
finally end
x:2
也就是说,try语句执行到return前将要返回的x值2 保存了,然后进入到finally,之后再回到try中返回之前保存的值,也就是说不管finally中你x怎么变化,返回的值都是返回之前保存的,与后来发生的改变无关。

好的,再看下面这个例子:
例2:
public class TrycatchTest {
    public static void main(String[] args) {
        System.out.println("x:" + new TrycatchTest().test());
    }

    int test() {
        int x = 1;
        try {
            System.out.println("try start");
            x++;
            return x;
        } finally {
            System.out.println("finally start");
            x++;
            return x;
        }
    }
}
代码跟上面基本一样,只不过在finally中加了一条 return x ;  猜猜这时候返回值为多少?
输出结果:
try start
finally start
x:3
解析: try中x自加后为2,这时候看到了return语句保存了当前的值2,然后进入了finally代码块,x自加为3。关键来了,这时候多了条return语句。首先第一个问题:这条语句执行吗?第二:还会回到try吗? 根据java的设计原则,finally中的语句必须是要执行的,所以finally中的return自然也会作为一条正常语句执行掉,那么还会回到try吗? 代码块都已经return结束掉了,还会再回去return一条吗?对吧,明显是不能的。 那么x值为多少,在当下的finally中x值为3然后直接给return回去了,所以是3。

再看看下面的例子:
例3:
public class TrycatchTest {
    public static void main(String[] args) {
        System.out.println("x:" + new TrycatchTest().test2());
    }
    int test2() {
        int x = 1;
        try {
            x++;
            if (x == 2) {
                System.out.println("try x:" + x);
                throw new Exception();
            }
            return x;
        } catch (Exception e) {
            x++;
            System.out.println("catch:"+x);
            return x;
        } finally {
            x++;
        }
    }
}
代码也很简单,加了个catch代码块,在try中将异常抛出就会进入到catch代码块中,注意这时候在catch中加入了条return语句,那么结果是多少? 
try x:2
catch:3
x:3
看到没,也是之前的规则:首先try中x自加为2,然后抛出了异常进入到catch,自加为3,这时候当想要return的时候将3的值暂存下来,进入到finally,finally中的x值自加为4, 然后回到try,return掉之前保存的值3.

好了,估计你也多少明白了,每次我们要return前,都会将return的值都暂时保存下来,然后为了符合java设计规范我们不得不临时跳到其他代码块处理些事情,处理完再把我们暂存的值给返回; 如果我们跳到其他代码块的时候遇到了return语句,就直接执行了那个代码块中的返回语句了,之前保存的值无效。 最后再看一个例子:
例4:
public class TrycatchTest {
    public static void main(String[] args) {
        System.out.println("x:" + new TrycatchTest().test2());
    }
    int test2() {
        int x = 1;
        try {
            x++;
            if (x == 2) {
                System.out.println("try x:" + x);
                throw new Exception();
            }
            return x;
        } catch (Exception e) {
            x++;
            System.out.println("catch:"+x);
            return x;
        } finally {
            x++;
            System.out.println("finally x:"+x);
            return x;
        }
    }
}
在finally中加了条return语句,聪明的你此刻一定能猜出最终返回值是多少了,没错,就是4。 看下面的结果:
try x:2
catch:3
finally x:4
x:4

如果你能猜出来,代表你已经掌握了,如果还不清楚,再看看我上边写的,应该没多大问题。带有return 的try catch在面试题中经常会出现,掌握一下还是有必要了。这里我并没有从Java源码和设计的角度谈try catch,之前看了《Java编程思想》以及一些其他的讲try catch的文章,感觉讲的并不是很清晰,就自己写代码测试了下,通过实践以及自己之前的所看的分析了下,希望能对你有帮助。 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值