java 重新拋出異常


一.有時希望把剛捕獲的異常重新拋出,尤其時在使用Exception捕獲所以異常的時候,既然已經得到了對當前異常對象的引用,可以重新把它拋出:

catch(Exception e){
      System.out.println("An exception was thrown");
     throw e;
}

二 :

1.重新拋出異常會把異常拋給上一級環境中的異常處理程序,同一個try塊的后續catch字句將忽略.

2.異常對象的所有信息都得以保持,所以高一級環境中捕獲此異常的處理程序可以從這個異常對象中得到所有消息.

3.如果只是把當前異常對象重新拋出,那么printStackTrace()方法顯示的將是原來異常拋出點的調用棧信息,而非重新拋出點的信息.

想要更新這個信息,可以調用fillInStackTrace()方法,這將返回一個Throwable對象,它是通過把當前調用棧信息填入原來那個異常對象而建立的.

package exceptions;
//: exceptions/Rethrowing.java
// Demonstrating fillInStackTrace()

public class Rethrowing {
  public static void f() throws Exception {
    System.out.println("originating the exception in f()");
    throw new Exception("thrown from f()");
  }
  public static void g() throws Exception {
    try {
      f();
    } catch(Exception e) {
      System.out.println("Inside g(),e.printStackTrace()");
      e.printStackTrace(System.out);
      throw e;
    }
  }
  public static void h() throws Exception {
    try {
      f();
    } catch(Exception e) {
      System.out.println("Inside h(),e.printStackTrace()");
      e.printStackTrace(System.out);
      throw (Exception)e.fillInStackTrace();
    }
  }
  public static void main(String[] args) {
    try {
      g();
    } catch(Exception e) {
      System.out.println("main: printStackTrace()");
      e.printStackTrace(System.out);
    }
    try {
      h();
    } catch(Exception e) {
      System.out.println("main: printStackTrace()");
      e.printStackTrace(System.out);
    }
  }
} /* Output:
originating the exception in f()
Inside g(),e.printStackTrace()
java.lang.Exception: thrown from f()
        at Rethrowing.f(Rethrowing.java:7)
        at Rethrowing.g(Rethrowing.java:11)
        at Rethrowing.main(Rethrowing.java:29)
main: printStackTrace()
java.lang.Exception: thrown from f()
        at Rethrowing.f(Rethrowing.java:7)
        at Rethrowing.g(Rethrowing.java:11)
        at Rethrowing.main(Rethrowing.java:29)
originating the exception in f()
Inside h(),e.printStackTrace()
java.lang.Exception: thrown from f()
        at Rethrowing.f(Rethrowing.java:7)
        at Rethrowing.h(Rethrowing.java:20)
        at Rethrowing.main(Rethrowing.java:35)
main: printStackTrace()
java.lang.Exception: thrown from f()
        at Rethrowing.h(Rethrowing.java:24)
        at Rethrowing.main(Rethrowing.java:35)
*///:~

三. 有可能在捕獲異常后拋出了另一種異常,這么做的話,得到的效果類似於使用fillInStackTrace(),有關原來異常發生點的信息會丟失,剩下的是與新的拋出點有關的信息:

//最后的那個異常僅知道自己來自main(),而對f()一無所知
package
exceptions; //: exceptions/RethrowNew.java // Rethrow a different object from the one that was caught. class OneException extends Exception { public OneException(String s) { super(s); } } class TwoException extends Exception { public TwoException(String s) { super(s); } } public class RethrowNew { public static void f() throws OneException { System.out.println("originating the exception in f()"); throw new OneException("thrown from f()"); } public static void main(String[] args) { try { try { f(); } catch(OneException e) { System.out.println( "Caught in inner try, e.printStackTrace()"); e.printStackTrace(System.out); throw new TwoException("from inner try"); } } catch(TwoException e) { System.out.println( "Caught in outer try, e.printStackTrace()"); e.printStackTrace(System.out); } } } /* Output: originating the exception in f() Caught in inner try, e.printStackTrace() OneException: thrown from f() at RethrowNew.f(RethrowNew.java:15) at RethrowNew.main(RethrowNew.java:20) Caught in outer try, e.printStackTrace() TwoException: from inner try at RethrowNew.main(RethrowNew.java:25) *///:~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM