Java Throwable getStackTrace() 方法



描述

Java Throwable getStackTrace() 方法返回一個堆疊跟蹤元素陣列,每個元素代表一個堆疊幀。陣列的第零個元素(假設陣列長度非零)表示堆疊頂部,這是序列中的最後一個方法呼叫。這是建立並丟擲此可丟擲物件的位置。

陣列的最後一個元素(假設陣列長度非零)表示堆疊底部,這是序列中的第一個方法呼叫。

宣告

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

public StackTraceElement[] getStackTrace()

引數

返回值

此方法返回一個堆疊跟蹤元素陣列,表示與此可丟擲物件相關的堆疊跟蹤。

異常

示例:獲取 Throwable 的堆疊跟蹤

以下示例演示了 Java Throwable getStackTrace() 方法的用法。我們定義了一個 raiseException() 方法,該方法在設定堆疊跟蹤後丟擲一個 Throwable。在主方法中,呼叫 raiseException() 方法,在 catch 塊中使用 getStackTrace() 獲取並列印異常堆疊跟蹤。

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         raiseException();
      } catch(Throwable e) {
         // access to the stack trace
         StackTraceElement[] trace = e.getStackTrace();
         System.err.println(trace[0].toString());
      }
   }
  
   public static void raiseException()throws Throwable {

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

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

輸出

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

ClassName.methodName(fileName:10)

示例:獲取 Exception 的堆疊跟蹤

以下示例演示了 Java Throwable setStackTrace() 方法的用法。我們定義了一個 raiseException() 方法,該方法在設定堆疊跟蹤後丟擲一個 Exception。在主方法中,呼叫 raiseException() 方法,在 catch 塊中使用 getStackTrace() 方法獲取並列印異常堆疊跟蹤。

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         raiseException();
      } catch(Throwable e) {
         // access to the stack trace
         StackTraceElement[] trace = e.getStackTrace();
         System.err.println(trace[0].toString());
      }
   }
  
   public static void raiseException()throws Exception {

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

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

輸出

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

ClassName.methodName(fileName:10)

示例:獲取 RuntimeException 的堆疊跟蹤

以下示例演示了 Java Throwable setStackTrace() 方法的用法。我們定義了一個 raiseException() 方法,該方法在設定堆疊跟蹤後丟擲一個 RuntimeException。在主方法中,呼叫 raiseException() 方法,在 catch 塊中使用 getStackTrace() 方法獲取並列印異常堆疊跟蹤。

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         raiseException();
      } catch(Throwable e) {
         // access to the stack trace
         StackTraceElement[] trace = e.getStackTrace();
         System.err.println(trace[0].toString());
      }
   }
  
   public static void raiseException()throws RuntimeException {

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

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

輸出

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

ClassName.methodName(fileName:10)
java_lang_throwable.htm
廣告