java 异常处理

try  中抛出异常,但如果finally 中也抛出异常的话,try中的异常信息会被忽略掉。例如


1 。异常信息丢失案例


@Test(expected=ArithmeticException.class)
public void test_1(){
//异常信息丢失案例
try {
Integer.parseInt("hello");
} catch (NumberFormatException e) {
log.info("{excetion:}",e);
throw  e; //这个异常被下面的掩盖了。
}finally{
int a = 12 / 0; //expected=ArithmeticException.class 这里的异常
}
}

上面的测试用例通过,可见抛出的异常不是try中的NumberFormatException,而是finally 中的ArithmeticException。

try中的异常信息被丢弃。


2. 解决方法之一:

@Test(expected=NumberFormatException.class)
public void test_2(){
//异常信息丢失案例:解决方法
try {
Integer.parseInt("hello");
} catch (NumberFormatException e) {
log.info("{excetion:}",e);
throw e; //这个异常被下面的掩盖了。
}finally{
try {
int a = 12 / 0;
} catch (Exception e) {
// 忽略此处异常,上面的主要根源异常就可以跑出去了
}
}
}


测试代码通过,expected=NumberFormatException.class 就是try中抛出的异常,因为try中的异常是根源,finally中的异常就丢弃吧。。


3.。解决方法之二:

@Test(expected=RuntimeException.class)
public void test_3(){
//异常信息丢失案例:解决方法
RuntimeException exception = new RuntimeException("error:");
try {

Integer.parseInt("hello");
} catch (NumberFormatException e) {

exception.addSuppressed(e); //把这个异常堆栈信息保留一下

}finally{
try {
int a = 12 / 0;
} catch (Exception e) {
exception.addSuppressed(e);
log.info("{excetion:}",exception);
throw exception;
}
}
}

利用addSuppressed 方法,把异常堆栈信息保留起来,带出去。

看一下log日志,是不少异常信息都有了:


17:07:43.939 [main] INFO  com.github.jdk7.Excetion1 - {excetion:}
java.lang.RuntimeException: error:
at com.github.jdk7.Excetion1.test_3(Excetion1.java:44) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_11]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_11]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_11]
at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_11]
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) [junit-4.11.jar:na]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.11.jar:na]
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) [junit-4.11.jar:na]
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.11.jar:na]
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) [junit-4.11.jar:na]
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) [junit-4.11.jar:na]
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) [junit-4.11.jar:na]
at org.junit.runners.ParentRunner.run(ParentRunner.java:309) [junit-4.11.jar:na]
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]
Suppressed: java.lang.NumberFormatException: For input string: "hello"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_11]
at java.lang.Integer.parseInt(Integer.java:580) ~[na:1.8.0_11]
at java.lang.Integer.parseInt(Integer.java:615) ~[na:1.8.0_11]
at com.github.jdk7.Excetion1.test_3(Excetion1.java:47) ~[classes/:na]
... 24 common frames omitted
Suppressed: java.lang.ArithmeticException: / by zero
at com.github.jdk7.Excetion1.test_3(Excetion1.java:54) ~[classes/:na]
... 24 common frames omitted


看到了吧。Suppressed: java.lang.NumberFormatException: For input string: "hello" 这行,就是try中的异常。

你可能感兴趣的:(java,java不一样的基础)