测试代码:java
/** * 测试异常抛出及捕捉 */ @Test public void test() { try { this.testA(); } catch (Exception ex) { System.out.println(CoreUtils.exceptionToString(ex)); } } /** * 测试测试 */ private void testA() { try { String a = null; a.length(); } catch (Exception ex) { //各类继续抛出异常的方式 } }
1. throw ex测试
java.lang.NullPointerException com.differ.jackyun.jackyunassservice.service.others.HgwTest.testA(HgwTest.java:35) com.differ.jackyun.jackyunassservice.service.others.HgwTest.test(HgwTest.java:25) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) ......
2. throw new RuntimeException(ex)this
java.lang.RuntimeException: java.lang.NullPointerException com.differ.jackyun.jackyunassservice.service.others.HgwTest.testA(HgwTest.java:38) com.differ.jackyun.jackyunassservice.service.others.HgwTest.test(HgwTest.java:25) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) ......
3. throw new RuntimeException("执行某个功能发生异常", ex)lua
java.lang.RuntimeException: 执行某个功能发生异常 com.differ.jackyun.jackyunassservice.service.others.HgwTest.testA(HgwTest.java:41) com.differ.jackyun.jackyunassservice.service.others.HgwTest.test(HgwTest.java:26) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) ......
4. throw new RuntimeException("执行某个功能发生异常:" + CoreUtils.exceptionToString(ex))spa
java.lang.RuntimeException: 执行某个功能发生异常:java.lang.NullPointerException com.differ.jackyun.jackyunassservice.service.others.HgwTest.testA(HgwTest.java:35) com.differ.jackyun.jackyunassservice.service.others.HgwTest.test(HgwTest.java:25) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) ......
总结:第3种方式抛出的异常(即:throw new RuntimeException("执行某个功能发生异常", ex)),会丢失关键的异常缘由,故开发时慎用!!!code