码迷,mamicode.com
首页 > 编程语言 > 详细

Java异常处理机制的秘密

时间:2014-05-30 23:42:06      阅读:520      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

一.结论

这些结论你可能从未听说过,但其正确性是毋庸置疑的,不妨先看看:

1.catch中throw不一定能抛回到上一层,因为finally中的return会抑制这个throw
2.finally中throw一定能抛回上一层,因为此时其后的return不会被执行到(throw中断了正常的顺序流)
3.在try/catch中return并不会直接返回上一层,而是先执行finally再返回

二.一段小程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package ayqy;
 
public class catchEx {
 
    /**
     * 测试Java异常处理机制<br />
     * 结论:<br />
     * 1.catch中throw不一定能抛回到上一层,因为finally中的return会抑制这个throw<br />
     * 2.finally中throw一定能抛回上一层,因为此时其后的return不会被执行到(throw中断了正常的顺序流)<br />
     * 3.在try/catch中return并不会直接返回上一层,而是先执行finally再返回
     */
    public static void main(String[] args){
        // TODO Auto-generated method stub
        try{
            System.out.println("Main_try");
            fun();
        }catch(Exception e){
            System.out.println("Main_catch");
        }finally{
            System.out.println("Main_finally");
        }
    }
     
    public static void fun() throws Exception
    {
        try{
            System.out.println("Fun_try");
            int a = 3 / 0;
        }catch(Exception ex){
            System.out.println("Fun_catch");
            throw ex;//#1
        }finally{
            System.out.println("Fun_finally");
            return;//#2
        }
    }<br><span style="line-height: 1.5;">}</span>

三.说明

http://hygj0113ldp.blog.163.com/blog/static/12253543620098308103964/

这个问题来自上面的链接博文,但原作者并没有给出合理的解释
原问题:
当 catch中抛出了未捕获的异常ex 并且 finally中有return 时,我们发现这个ex并不能被抛出到上一层方法,这是为什么呢?
实验:
1.直接运行上面的程序
结果是:
Fun_catch
Fun_finally
Main_finally
正如问题描述里写的,为什么没有输出Main_catch?
2.注释#1语句
结果不变,也就是说,抛不抛异常效果一样,换言之,这个异常并没有成功地抛出去
3.取消注释#1语句,注释#2语句
结果是:
Main_try
Fun_try
Fun_catch
Fun_finally
Main_catch
Main_finally
和我们预想的结果一致,那么不难得出结论:
finally中的return能够抑制(或着说掩盖、消费)在此之前抛出的异常(try/catch中的未捕获异常)

Java异常处理机制的秘密,布布扣,bubuko.com

Java异常处理机制的秘密

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/ayqy/p/3761476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!