Java异常处理机制的秘密

一.结论

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

1.catch中throw不一定能抛回到上一层,因为finally中的return会抑制这个throw

2.finally中throw一定能抛回上一层,因为此时其后的return不会被执行到(throw中断了正常的顺序流)

3.在try/catch中return并不会直接返回上一层,而是先执行finally再返回

二.一段小程序

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
		}
	}

三.说明

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中的未捕获异常)

posted @ 2014-05-30 17:08  梦烬  阅读(405)  评论(0编辑  收藏  举报