首页 > 代码库 > 4.异常捕获后再次抛出

4.异常捕获后再次抛出

  • 4.异常捕获后再次抛出

  • 情况一:捕获后抛出原来的异常,希望保留最新的异常抛出点--fillStackTrace

  • 情况二:捕获后抛出新的异常,希望抛出完整的异常链--initCause

  • 1.捕获后重新抛出捕获的异常
  • 在函数中捕获了异常,在catch模块中不做进一步的处理,而是向上一级进行传递
  • catch(Exception e){
  •      throw e;
  • }
public class ReThrow {
    public static void f()throws Exception{
        throw new Exception("Exception: f()");
    }

    public static void g() throws Exception{
        try{
            f();
        }catch(Exception e){
            System.out.println("inside g()");
            throw e;
        }
    }
    public static void main(String[] args){
        try{
            g();
        }
        catch(Exception e){
            System.out.println("inside main() function");
            e.printStackTrace(System.out);
        }
    }
}

 运行结果如下:

inside g()
inside main() function
java.lang.Exception: Exception: f()
    at ReThrow.f(ReThrow.java:6)   //异常的抛出点还是最初抛出异常的函数f()
    at ReThrow.g(ReThrow.java:11)
    at ReThrow.main(ReThrow.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
 
  • 2.fillStackTrace——覆盖前边的异常抛出点(获取最新的异常抛出点)
  • 在此抛出异常的时候进行设置
  • catch(Exception e){
  •      (Exception)e.fillInStackTrace();
  • }
public static void g() throws Exception{
    try{
        f();
    }catch(Exception e){
        System.out.println("inside g()");
        throw (Exception)e.fillInStackTrace();
    }
}

 运行结果如下:

inside g()
inside main() function
java.lang.Exception: Exception: f()
    at ReThrow.g(ReThrow.java:14)         //显示的就是最新的抛出点
    at ReThrow.main(ReThrow.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
 
  • 3.捕获异常后抛出新的异常( 区别于捕获异常之后重新抛出)
  • 方式1:Exception e=new Exception();----important
  • e.initCause(ex);
  • 方式2:
  • Exception e =new Exception(ex);
  • public void f(){
        try{
             g();//throws NullPointException 
         }catch(NullPointException ex){
             ex.printStasckTrace();
             Exception e=new Exception();
             e.initCause(ex);//就可以看到原始的异常保留下来
             throw e;
         }
    }
  • 运行结果:
  • java.lang.Exception
  •     at Main.g(Main.java:14)
  •     at Main.main(Main.java:22)
  •     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  •     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  •     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  •     at java.lang.reflect.Method.invoke(Method.java:606)
  •     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
  • Caused by: java.lang.NullPointerException
  •     at Main.f(Main.java:6)
  •     at Main.g(Main.java:11)
  •     ... 6 more
 
 
 
 
 
 
 
 
 
 
 

4.异常捕获后再次抛出