Java Throwable printStackTrace() 方法



描述

Java Throwable printStackTrace() 方法將此 throwable 及其回溯列印到標準錯誤流。它在錯誤輸出流上列印此 Throwable 物件的堆疊跟蹤,該錯誤輸出流是欄位 System.err 的值。

宣告

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

public void printStackTrace()

引數

返回值

此方法不返回值。

異常

示例:列印 Throwable 的堆疊跟蹤

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

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         raiseException();
      } catch(Throwable e) {
         // prints stacktrace for this Throwable Object
         e.printStackTrace();
      }
   }
  
   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;
   }
} 

輸出

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

java.lang.Throwable: This is new Exception...
at ClassName.methodName(fileName:5)

示例:列印 Exception 的堆疊跟蹤

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

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) {

      try {
         raiseException();
      } catch(Throwable e) {
         // prints stacktrace for this Throwable Object
         e.printStackTrace();
      }
   }
  
   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;
   }
} 

輸出

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

java.lang.Exception: This is new Exception...
at ClassName.methodName(fileName:5)

示例:列印 RuntimeException 的堆疊跟蹤

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

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) {

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

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

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

輸出

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

java.lang.RuntimeException: This is new Exception...
at ClassName.methodName(fileName:5)
java_lang_throwable.htm
廣告

© . All rights reserved.