Try catch finally 与 return 结合使用执行顺序所有情况分析

首先对按钮添加点击事件,事件代码为:System.out.println(test());

一、无异常时

1、			
private String test(){
        try {
            System.out.println("try...");   
        } catch (Exception e) {
            e.getMessage();
            System.out.println("catch..."); 
        } finally {
            System.out.println("finally..."); 
        }
        return "return at last...";
    }
运行结果:
2019-07-15 10:21:52.049 22618-22618/com.example.mymvp I/System.out: try...
2019-07-15 10:21:52.049 22618-22618/com.example.mymvp I/System.out: finally...
2019-07-15 10:21:52.049 22618-22618/com.example.mymvp I/System.out: return at last...

2、
private String test(){
        try {
            System.out.println("try...");
            return "return at try..."; 
        } catch (Exception e) {
            e.getMessage();
            System.out.println("catch...");
        } finally {
            System.out.println("finally...");
        }
        return "return at last...";
    }
运行结果:
2019-07-15 10:25:13.605 22816-22816/com.example.mymvp I/System.out: try...
2019-07-15 10:25:13.605 22816-22816/com.example.mymvp I/System.out: finally...
2019-07-15 10:25:13.605 22816-22816/com.example.mymvp I/System.out: return at try...

3、
private String test(){
        try {
            System.out.println("try...");
            return "return at try...";
        } catch (Exception e) {
            e.getMessage();
            System.out.println("catch...");
        } finally {
            System.out.println("finally...");
            return "return at finally...";
        }
    }
运行结果:
2019-07-15 10:26:35.815 23033-23033/com.example.mymvp I/System.out: try...
2019-07-15 10:26:35.815 23033-23033/com.example.mymvp I/System.out: finally...
2019-07-15 10:26:35.815 23033-23033/com.example.mymvp I/System.out: return at finally...

结论:无异常时,首先执行try代码块,如果try代码块有return则在执行return语句前跳转到finally代码块执行,如果无return,则执行完整个try代码块后跳转到finally代码块,执行finally代码块,如果finally代码块有return,则执行return返回,如果没有则执行完finally代码块后回到try的return语句处执行并返回。

二、有异常时

1、
   private String test(){
        try {
            System.out.println("try...");
            System.out.println("exception before...");
            int i = 1/0;
            System.out.println("exception after...");
        } catch (Exception e) {
            e.getMessage();
            System.out.println("catch...");
        } finally {
            System.out.println("finally...");
        }
        return "return at last...";
}
执行结果
2019-07-15 10:29:27.109 23259-23259/com.example.mymvp I/System.out: try...
2019-07-15 10:29:27.109 23259-23259/com.example.mymvp I/System.out: exception before...
2019-07-15 10:29:27.110 23259-23259/com.example.mymvp I/System.out: catch...
2019-07-15 10:29:27.110 23259-23259/com.example.mymvp I/System.out: finally...
2019-07-15 10:29:27.110 23259-23259/com.example.mymvp I/System.out: return at last...

2、
    private String test(){
        try {
            System.out.println("try...");
            System.out.println("exception before...");
            int i = 1/0;
            System.out.println("exception after...");
            return "return at try...";
        } catch (Exception e) {
            e.getMessage();
            System.out.println("catch...");
            return "return at catch...";
        } finally {
            System.out.println("finally...");
        }
    }
执行结果
2019-07-15 10:31:12.413 23600-23600/com.example.mymvp I/System.out: try...
2019-07-15 10:31:12.413 23600-23600/com.example.mymvp I/System.out: exception before...
2019-07-15 10:31:12.413 23600-23600/com.example.mymvp I/System.out: catch...
2019-07-15 10:31:12.413 23600-23600/com.example.mymvp I/System.out: finally...
2019-07-15 10:31:12.413 23600-23600/com.example.mymvp I/System.out: return at catch...

3、
   private String test(){
        try {
            System.out.println("try...");
            System.out.println("exception before...");
            int i = 1/0;
            System.out.println("exception after...");
            return "return at try...";
        } catch (Exception e) {
            e.getMessage();
            System.out.println("catch...");
            return "return at catch...";
        } finally {
            System.out.println("finally...");
            return "return at finally...";
        }
}
执行结果
2019-07-15 10:32:32.494 23784-23784/com.example.mymvp I/System.out: try...
2019-07-15 10:32:32.494 23784-23784/com.example.mymvp I/System.out: exception before...
2019-07-15 10:32:32.494 23784-23784/com.example.mymvp I/System.out: catch...
2019-07-15 10:32:32.494 23784-23784/com.example.mymvp I/System.out: finally...
2019-07-15 10:32:32.494 23784-23784/com.example.mymvp I/System.out: return at finally...

4、
private String test(){
        try {
            System.out.println("try...");
            System.out.println("exception before...");
            int i = 1/0;
            System.out.println("exception after...");
            return "return at try...";
        } catch (Exception e) {
            e.getMessage();
            System.out.println("catch...");
        } finally {
            System.out.println("finally...");
        }
        return "return at last...";
}
结果:
2019-07-22 15:18:17.325 7549-7549/com.example.mymvp I/System.out: try...
2019-07-22 15:18:17.325 7549-7549/com.example.mymvp I/System.out: exception before...
2019-07-22 15:18:17.326 7549-7549/com.example.mymvp I/System.out: catch...
2019-07-22 15:18:17.326 7549-7549/com.example.mymvp I/System.out: finally...
2019-07-22 15:18:17.326 7549-7549/com.example.mymvp I/System.out: return at last...
如果catch和finally中没有return,而在try发生异常的后面有return时,try中的return并不执行,切记。

总结:如果有异常时,首先执行try代码块到发生异常的地方,然后执行catch代码块到return语句的前一行代码处(如果return存在),然后再执行finally代码块,如果finally中有return,则执行并返回,如果没有,再检查catch中是否有return,有则执行并返回,无则跳出try catch finally代码块。

 

总结:

整个try catch finally 与 return 结合使用的执行,首先按照try catch finally的顺序正常执行代码,这个过程中不执行return(如果存在return的话) ,执行完成后,再反向finaly catch try 的顺序去查找return,执行遇到的第一个return,然后返回,代码执行完成。

 

但是,依然有一个需要特别注意的地方,如下:

1、
public static String test() {
        String str = "A";
        try {
            str = "B";
            return "the return is: " + str;
        } finally {
            System.out.println("finally change return string to C");
            str = "C";
            //return "the return is" + str;
        }
}
执行结果:
    finally change return string to C
    the return is: B
表示finally中的代码str = "C";并没有生效。

如果:
2、
public static String test() {
        String str = "A";
        try {
            str = "B";
            //return "the return is: " + str;
        } finally {
            System.out.println("finally change return string to C");
            str = "C";
            return "the return is: " + str;
        }
}
执行结果:
    finally change return string to C
    the return is: C
表示finally中的代码修改str = "C";生效了

表示,如果finally中修改某变量,如果return在finally中,则该修改生效,如果return在try中,则该修改并不生效,需要特别注意。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值