finally语句与return语句详解

1.return语句执行顺序

1.1finally语句是在return语句执行之后,return语句返回之前执行的

package exception;

public class Demo06 {
    public static void main(String[] args) {
        System.out.println(func());
    }

    public static int func(){
        int a = 10;
        try{
            System.out.println("try中的代码块");
            return a += 10;
        }catch (Exception e){
            System.out.println("catch中的代码块");
        }finally {
            System.out.println("finally中的代码块");
            if(a > 10){
                System.out.println("a > 10,"+"a="+a);
            }
        }
        return a += 50;
    }
}

运行结果:

try中的代码块
finally中的代码块
a > 10,a=20
20

注意:

a > 10,a=20的结果说明了return a += 10已经执行了,但是没有直接返回,而是先去执行finally语句的内容,然后再去返回结果

2.覆盖问题

2.1finally块中的return语句会覆盖try块的return返回

package exception;

public class Demo07 {
    public static void main(String[] args) {
        System.out.println(func());
    }

    public static int func(){
        int a = 10;
        try{
            System.out.println("try中的代码块");
            return a += 10;
        }catch (Exception e){
            System.out.println("catch中的代码块");
        }finally {
            System.out.println("finally中的代码块");
            if(a > 10){
                System.out.println("a>10,"+"a="+a);
            }
            return 100;
        }
    }
}

运行结果:

try中的代码块
finally中的代码块
a>10,a=20
100

注意:

(1)如果try中有return语句,finally中也有return语句,最终执行的是finally中的return语句
(2)如果finally代码块中写了return语句,那么finally之后的return语句就变成不可到达的语句,需要注释掉,否则编译不过

2.2如果finally语句没有return语句覆盖返回值,那么原来的返回值可能因为finally里的修改而改变也有可能不变

(1)测试1

package exception;

public class Demo08 {
    public static void main(String[] args) {
        System.out.println(func());
    }

    public static int func(){
        int a = 10;
        try{
            System.out.println("try中的代码块");
            return a += 20;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("catch中的代码块");
        }finally {
            System.out.println("finally中的代码块");
            a  += 20;
            if(a > 10){
                System.out.println("a > 10,a="+a);
            }
            a += 20;
        }
        return 200;
    }
}

运行结果:

try中的代码块
finally中的代码块
a > 10,a=50
30

注意:

对于基本数据类型来说,finally中对返回值的修改不会影响try中的返回变量的值

(2)测试2

package exception;

import java.util.HashMap;
import java.util.Map;

public class Demo09 {
    public static void main(String[] args) {
        System.out.println(getMap().get("KEY").toString());
    }

    public static Map<String,String> getMap(){
        Map<String,String> map = new HashMap<>();
        map.put("KEY","INIT");
        try{
            map.put("KEY","try");
            return map;
        }catch (Exception e){
            e.printStackTrace();
            map.put("KEY","catch");
        }finally {
            map.put("KEY","finally");
            map = null;
        }
        return map;
    }
}

运行结果:

finally

注意:

对于引用数据类型来说,finally中对返回值的修改会影响try中的返回变量的值

3.异常情况

3.1try块中的return语句在异常的情况下不会被执行

package exception;

public class Demo10 {
    public static void main(String[] args) {
        System.out.println(func());
    }

    public static int func(){
        int a = 10;
        try{
            System.out.println("try中的代码块");
            a = a/0;
            return a += 50;
        }catch (Exception e){
            a += 15;
            System.out.println("catch中的代码块");
        }finally {
            System.out.println("finally中的代码块");
            if(a > 20){
                System.out.println("a > 20,a ="+a);
            }
            a += 10;
        }
        return a;
    }
}

运行结果:

try中的代码块
catch中的代码块
finally中的代码块
a > 20,a =25
35

注意:

try语句块中发生异常,try语句异常后的内容不会执行,return语句也不会执行,执行的是捕获到的catch语句块和finally语句块

3.2try中发生异常时,return写在catch语句中

package exception;

public class Demo11 {
    public static void main(String[] args) {
        System.out.println(func());
    }

    public static int func(){
        int a = 10;
        try{
            System.out.println("try中的代码块");
            a = a /0;
            return a += 10;
        }catch (Exception e){
            System.out.println("catch中的代码块");
            return a += 15;
        }finally {
            System.out.println("finally中的代码块");
            if (a > 10){
                System.out.println("a > 10, a = "+a);
            }
            a += 50;
            System.out.println(a);
        }
    }
}


运行结果:

try中的代码块
catch中的代码块
finally中的代码块
a > 10, a = 25
75
25

注意:

try中发生异常之后,catch中的return语句先执行,确定了返回值之后(保存起来,finally中的语句对返回值无影响)再去finally语句块,执行完之后再返回a的值,finally中对a的修改对返回值无效

4.finally语句一定会被执行吗?

(1)当程序进入try语句之前就出现异常时,会直接结束
(2)try语句块中有强制退出时也不会执行finally语句块中的代码

System.exit(0);

代码示例:

package exception;

public class Demo12 {
    public static void main(String[] args) {
        int a = 10;
        try{
            System.out.println("try block");
            System.exit(0);
        }catch (Exception e){
            System.out.println("catch block");
        }finally {
            System.out.println("finally block");
        }
    }
}

运行结果:

try block
  • 45
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值