finally和return的执行顺序

finally:异常代码块中,最后都会执行的代码。通常用来关闭资源。

return:用于跳出当前方法,指针返回值。

这篇文章主要是为了验证,当我们的异常代码块中,在finally代码前就已经return。那么会任何执行呢?我列出以下几个场景来验证。

1.try...catch中有return,finally里面的语句会执行吗?

如下是try中有return的执行情况。

public class main {
    public static int show() {
        try {
            return 1;
        }finally{
            System.out.println("finally模块被执行");
        }
    }
    public static void main(String args[]) {
        System.out.println(show());
    }
}

输出如下:

finally模块被执行
1

解释:当finally前有return语句时,finally代码块也会被执行。如上代码,加入try里面有10行代码,最后一行是return。那么java会执行前面9行代码,在跳到finally里面执行代码。最后在跳回到try里面的return。

 

如下是catch中有return的执行情况

public class main {
    public static int show() {
        try {
            int a = 8/0;
            return 1;
        }catch (Exception e) {
            return 2;
        }finally{
            System.out.println("finally模块被执行");
        }
    }
    public static void main(String args[]) {
        System.out.println(show());
    }
}

输出如下:

finally模块被执行
2

解释:和try中有return的情况差不多。当发生了异常,就会执行catch中的return。而不会管try里的。

2.如果try...cathch,finally中都有return,会返回什么值

public class main {
    public static int show() {
        try {
            int a = 8/0;
            return 1;
        }catch (Exception e) {
            return 2;
        }finally{
            System.out.println("finally模块被执行");
            return 0;
        }
    }
    public static void main(String args[]) {
        System.out.println(show());
    }

}

输出如下:

finally模块被执行
0

解释:finally中有return则会直接输出,不会再去执行try...catch中的。

3.finally中对于返回变量做的改变会影响最终的返回结果吗

这里需要举出3个例子来说明。

 

1.当变量的值为基础类型时,不会影响最终的返回结果。

public class tryDemo {
    public static int show() {
        int result = 0;
        try {
            return result;
        }finally{
            System.out.println("finally模块被执行");
            result = 1;
        }
    }
    public static void main(String args[]) {
        System.out.println(show());
    }
}

输出如下:

finally模块被执行
0

2.当变量为引用变量的时,会影响最终的返回结果。

public class main {
        public static String[] show() {
            String[] s = new String[2];
            try {
                s[0]="1";
                return s;
            }finally{
                s[1]="2";
            }
        }
        public static void main(String args[]) {
            System.out.println(Arrays.toString(show()));
        }
}

输出如下:

[1, 2]

3.比较特殊的一种就是,在finally中设置对象null无效。比如在obj=null前,对对象的任何操作,都是可以的,但是设置null值。他会返回最近的拷贝对象。

public class main {
        public static Object show() {
            Object obj = new Object();
            try {
                return obj;
            }finally{
                System.out.println("finally模块被执行");
                obj = null;
            }
        }
        public static void main(String args[]) {
            System.out.println(show());
        }
}

输出如下:

finally模块被执行
java.lang.Object@1540e19d

最后注意try...catch中不要使用return。会把异常吃掉。

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值