Java Throwable fillInStackTrace() 方法



描述

Java Throwable fillInStackTrace() 方法填充執行堆疊跟蹤。此方法在此 Throwable 物件中記錄有關當前執行緒的堆疊幀當前狀態的資訊。

宣告

以下是 java.lang.Throwable.fillInStackTrace() 方法的宣告

public Throwable fillInStackTrace()

引數

返回值

此方法返回對此 Throwable 例項的引用。

異常

示例:列印異常的堆疊跟蹤

以下示例顯示了 Java Throwable fillInStackTrace() 方法的使用。我們定義了兩個方法 function1(),它丟擲一個異常,以及 function2(),它呼叫 function1() 並處理異常,並使用 fillInStackTrace() 方法丟擲已處理異常的引用。在 main 方法中,呼叫 function2() 方法,並在 catch 塊中檢索並列印異常堆疊跟蹤。

package com.tutorialspoint;

public class ThrowableDemo {

   public static void function1() throws Exception {
      throw new Exception("this is thrown from function1()");
   }

   public static void function2() throws Throwable {
      try {
         function1();
      } catch(Exception e) {
         System.err.println("Inside function2():");
         e.printStackTrace();
         throw e.fillInStackTrace();
      }
   }

   public static void main(String[] args) throws Throwable {
      try {
         function2();
      } catch (Exception e) {
         System.err.println("Caught Inside Main:");
         e.printStackTrace();
      }
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Inside function2():
java.lang.Exception: this is thrown from function1()
at ThrowableDemo.function1(ThrowableDemo.java:4)
at ThrowableDemo.function2(ThrowableDemo.java:9)
at ThrowableDemo.main(ThrowableDemo.java:19)
Caught Inside Main:
java.lang.Exception: this is thrown from function1()
at ThrowableDemo.function2(ThrowableDemo.java:13)
at ThrowableDemo.main(ThrowableDemo.java:19)

示例:列印 Throwable 的堆疊跟蹤

以下示例顯示了 Java Throwable fillInStackTrace() 方法的使用。我們定義了一個方法 raiseException(),它在設定堆疊跟蹤後丟擲一個 Throwable。在 main 方法中,呼叫 raiseException() 方法,並在 catch 塊中使用 fillInStackTrace() 方法檢索並列印異常堆疊跟蹤。

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         raiseException();
      } catch(Throwable e) {
         // prints stacktrace for this Throwable Object
         throw e.fillInStackTrace();
      }
   }
  
   public static void raiseException() throws Throwable {

      Throwable t = new Throwable("This is new Exception...");
      StackTraceElement[] trace = new StackTraceElement[] {
         new StackTraceElement("ClassName","methodName","fileName",5)
      };

      // sets the stack trace elements
      t.setStackTrace(trace);
      throw t;
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Exception in thread "main" java.lang.Throwable: This is new Exception...
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:11)

示例:列印異常的堆疊跟蹤

以下示例顯示了 Java Throwable fillInStackTrace() 方法的使用。我們定義了一個方法 raiseException(),它在設定堆疊跟蹤後丟擲一個異常。在 main 方法中,呼叫 raiseException() 方法,並在 catch 塊中使用 fillInStackTrace() 方法檢索並列印異常堆疊跟蹤。

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         raiseException();
      } catch(Throwable e) {
         // prints stacktrace for this Throwable Object
         throw e.fillInStackTrace();
      }
   }
  
   public static void raiseException() throws Exception {

      Exception t = new Exception("This is new Exception...");
      StackTraceElement[] trace = new StackTraceElement[] {
         new StackTraceElement("ClassName","methodName","fileName",5)
      };

      // sets the stack trace elements
      t.setStackTrace(trace);
      throw t;
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Exception in thread "main" java.lang.Exception: This is new Exception...
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:11)
java_lang_throwable.htm
廣告