码迷,mamicode.com
首页 > 其他好文 > 详细

try ,finally都抛出异常如何处理.如果try中抛出了异常,在控制权转移到调用栈上一层代码之前, finally 语句块也会执行,如果finally抛出异常,try语句快抛出的那个异常就

时间:2014-12-15 09:03:26      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:java   exceptionalhandling   

package com.github.jdk7;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * try ,finally都抛出异常如何处理.如果try中抛出了异常,在控制权转移到调用栈上一层代码之前,
 * finally 语句块也会执行,如果finally抛出异常,try语句快抛出的那个异常就丢失了.
 * 
 * @author doctor
 *
 * @since 2014年12月14日 下午10:54:58
 */
public class ExceptionalHandling {
	private static final Logger log = LoggerFactory.getLogger(ExceptionalHandling.class);

	@Test(expected = ArithmeticException.class)
	public void test_wrong_way() {
		try {
			Integer.parseInt("hello");
		} catch (NumberFormatException e) {
			throw e;
		} finally {
			try {
				int a = 10 / 0;
			} catch (ArithmeticException e) {
				throw e;
			}
		}
	}

	/**
	 * 会得到的异常堆栈信息: 
	 * [main] ERROR com.github.jdk7.ExceptionalHandling -
	 * java.lang.NumberFormatException: For input string: "hello"
	 *         at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_25]
	 *         .............
	 *         Suppressed: java.lang.ArithmeticException: / by zero
			   at com.github.jdk7.ExceptionalHandling.test_right_way(ExceptionalHandling.java:43) ~[classes/:na]
	 */
	@Test(expected = NumberFormatException.class)
	public void test_right_way() {
		RuntimeException throwable = null;
		try {
			Integer.parseInt("hello");
		} catch (NumberFormatException e) {
			throwable = e;
		} finally {
			try {
				int a = 10 / 0;
			} catch (ArithmeticException e) {
				if (throwable == null) {
					throwable = e;
				} else {
					throwable.addSuppressed(e);
				}
			}
		}

		if (throwable != null) {
			log.error("", throwable);
			throw throwable;
		}
	}
}

try ,finally都抛出异常如何处理.如果try中抛出了异常,在控制权转移到调用栈上一层代码之前, finally 语句块也会执行,如果finally抛出异常,try语句快抛出的那个异常就

标签:java   exceptionalhandling   

原文地址:http://blog.csdn.net/doctor_who2004/article/details/41932133

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