JAVA主動拋異常的幾種方式及捕捉結果輸出對比

測試代碼: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

相關文章
相關標籤/搜索