在有try-catch-finally的方法存在返回值问题

    今天在写代码涉及到try-catch-finally中,io流需要关闭,在finally里面进行处理。那我的返回值放在哪里呢? try里面还是 try外面

这里写了一个demo,供参考使用:

public class Test {

    public static void main(String[] args) throws Exception {
        Test t = new Test();
//        System.out.println(t.t1());
//        System.out.println(t.t2());
        System.out.println(t.t3());
    }

    public String t1() throws Exception{
        try{

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            System.out.println("222");
        }
        Thread.sleep(1000);
        System.out.println("111");
        return "1";
    }

    public String t2() throws Exception{
        try{
            System.out.println("333");
            return "2";
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //在 return "2" 之前执行
            Thread.sleep(1000);
            System.out.println("222");
        }
        System.out.println("111");
        return "1";
    }

    public String t3() throws Exception{
        try{
            System.out.println("333");
            return "2";
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //在 return "2" 之前执行
            System.out.println("222");
            return "3";
        }
    }

    //这是我写 导入Excel 实现
    public String t4() throws Exception{
        try{
            return "2";
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //在 return "2" 之前执行
            System.out.println("222");
        }
    }
}

依次将,首先看 t1方法打印:

public String t1() throws Exception{
        try{

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            System.out.println("222");
        }
        Thread.sleep(1000);
        System.out.println("111");
        return "1";
    }

先打印finally里面的代码,再打印try-catch-finally 之后的代码,返回值为1

看t2方法打印如下:

    public String t2() throws Exception{
        try{
            System.out.println("333");
            return "2";
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //在 return "2" 之前执行
            Thread.sleep(1000);
            System.out.println("222");
        }
        System.out.println("111");
        return "1";
    }

结果的返回值为2 说明是在try里面返回的,说明 return "1" 不会走了,这里还是按照代码的顺序来执行的,但是 finally里面的语句是在 打印333之后执行的,也就说明finally的代码是在return返回之前执行 ,说明 finally是在return之前执行的(return一定在try里面).

 

那么这样看的话t3方法 打印就好解释了:

    public String t3() throws Exception{
        try{
            System.out.println("333");
            return "2";
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //在 return "2" 之前执行
            System.out.println("222");
            return "3";
        }
    }

finally里面有返回值,但finally是在return之前执行,那么 finally 就直接返回。

 

t4方法不能运行:这就是我之前犯的问题,在try里面返回 finally 控制流关闭 外面还是要有一个return值。为什么要有值呢?

如果try里面的代码抛出异常了,就不会走 try里面的return "2" 了 所以要在外面加一个 return

public String t4() throws Exception{
    try{
        int c = 1/0; //这里抛出异常了 后面果断不执行了呗s
        return "2";
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        //在 return "2" 之前执行
        System.out.println("222");
    }
    return  null;
}

综上,try里面存在异常的可能情况 会走 catch  那么可以在catch里面return ,否则在try里面return 即可

如下:

    public String t4() throws Exception{
        try{
            int c = 1/0;
            return "2";
        }catch(Exception e){
            System.out.println("333");
            return  "111";
        }finally{
            //在 return "2" 之前执行
            System.out.println("222");
        }
    }

先打印333 再打印222  最后打印111.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值