首页 > 其他 > 详细

TryCatchFinallyReturn

时间:2020-01-29 12:28:08      阅读:71      评论:0      收藏:0      [点我收藏+]
public class TryCatchFinallyReturnTest {
    public int test(){
        try {
            int i=1;
            int j=2/i;
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 2;
        }finally {
            return 3;
        }
        return 4;
    }

    public static void main(String[] args) {
        int ret=new TryCatchFinallyReturnTest().test();
        System.out.println(ret);
    }
}
  • 4是Unreachable statement,只有当try和catch的return二选一,且没有finally的return,才能编译通过,否则出现checked异常。
  • try和catch和finally都有return的情况下,最后return的一定是finally的return,try和catch先执行到如果有return会缓存起来,如果finally有return就会覆盖。

TryCatchFinallyReturn

原文:https://www.cnblogs.com/zxcoder/p/12239951.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!