详细讲解面试中常见的try、catch、finally 、return问题

本篇文章是小编第一次写的技术文章,小编也在成长为大牛的路上希望我的文章对能看到此文章的技术爱好者有所帮助,我会尽可能的把所有的情况都能说明,废话不多说进入正题吧!!!

1.try{}catch{}finally{}return 情况

1

public class Trycatch {

public int test(int a, int b){
    try {
        int m = a + b;
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("catch异常");
    } finally {
        System.out.println("finally执行代码");
    }
    return 0;
}

public static  void main(String[] args){
    Trycatch trycatch = new Trycatch();
    int n = trycatch.test(2,18);
    System.out.println(n);
}
}

最后的执行结果:
finally执行代码
0

2.try{}中有return又是什么情况呢?

public int test(int a, int b){
    try {
        int m = a + b;
        return m;
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("catch异常");
    } finally {
        System.out.println("finally执行代码");
    }
    return 0;
}

public static  void main(String[] args){
    Trycatch trycatch = new Trycatch();
    int n = trycatch.test(2,18);
    System.out.println(n);
}
}

结果为:
finally执行代码
20
说明当try代码块有return 时并不会影响finally代码块的执行,同时当执行到a + b 的时候程序将结果m 保存在一个临时栈中,当执行完finally代码块后从保存临时栈取出保存的结果并返回。
那么你肯定又会想思考如果我在finally 代码块将 a或b的值进行修改 会返回什么结果呢?

public class Trycatch {
public int test(int a, int b){
    try {
        int m = a + b;
        return m;
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("catch异常");
    } finally {
        System.out.println("finally执行代码");
        a = 1;
        b = 2;
    }
    return 0;
}

public static  void main(String[] args){
    Trycatch trycatch = new Trycatch();
    int n = trycatch.test(2,18);
    System.out.println(n);
}

}
最后结果依然是:
finally执行代码
20
这说明什么呢???说明finally 程序并没有影响到之前try代码块保存的结果。

3.try{}catch{return}finally{}return;

public class Trycatch {
    public int test(int a, int b){
        try {
            int m = a / b;
           // return m;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("catch异常");
            return a;
        } finally {
            System.out.println("finally执行代码");
        }
        return 0;
    }

    public static  void main(String[] args){
        Trycatch trycatch = new Trycatch();
        int n = trycatch.test(2,0);
        System.out.println(n);
    }
}

最后的结果为:
java.lang.ArithmeticException: / by zero
catch异常
at com.chen.leyou.test.Trycatch.test(Trycatch.java:10)
finally执行代码
at com.chen.leyou.test.Trycatch.main(Trycatch.java:24)
2
这里只考虑有异常的情况无异常的情况不做解释,自己动手试试
4.finally{}中有return

public class Trycatch {

    public int test(int a, int b){
        try {
            int m = a + b;
           // return m;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("catch异常");
            return a;
        } finally {
            System.out.println("finally执行代码");
            return  a - b;
        }
        //return 0;**当finally 有return 这里就不可以再有return 否则会报错**
    }

    public static  void main(String[] args){
        Trycatch trycatch = new Trycatch();
        int n = trycatch.test(2,0);
        System.out.println(n);
    }
}

结果:
finally执行代码
2
5.try{return}catch{return}finally{}
这个情况比较好理解有异常按照情况3执行么有异常按照情况2执行,这里就不在粘贴代码了自己动手吧。
6.try{return}catch{}finally{return}

public class Trycatch {

    public int test(int a, int b){
        try {
            int m = a + b;
           return m;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("catch异常");
            //return a;
        } finally {
            System.out.println("finally执行代码");
            return  a * b ;
        }
        //return 0;
    }
    public static  void main(String[] args){
        Trycatch trycatch = new Trycatch();
        int n = trycatch.test(2,0);
        System.out.println(n);
    }
}

结果:
finally执行代码
0
因为当执行a+b 后会将m的值保存在临时栈中,然后取值去执行finally中打印语句 最后发现fianlly中也有return 在这里就执行返回结果 而不是在再去执行try 代码块返回m 的值了
7.try{}catch{return}finally{return}
道理其实和6是一样的当有异常发生时会将catch 中的值保存在临时栈中,然后执行finally中语句 发现有return直接进行返回,这里就不进行代码展示了。
8.try{return;}catch(){return;}finally{return;}
无异常的情况执行6
有异常的情况执行7

9。什么情况下不会执行finally{}中的代码呢?
转载这个情况我在面试的时候被问到过,希望大家还是简单了解一下,最重要还是上面的8种情况,如何有问题欢迎留言!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值