Java Throwable printStackTrace() 方法



描述

Java Throwable printStackTrace(PrintStream s) 方法將此可丟擲物件及其回溯資訊列印到指定的列印流。

宣告

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

public void printStackTrace(PrintStream s)

引數

s - 這是用於輸出的 PrintStream

返回值

此方法不返回值。

異常

示例:列印 Throwable 的堆疊跟蹤

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

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {

      OutputStream out;
      try {
         raiseException();
      } catch(Throwable e) {
         out = new FileOutputStream("file.text");
         // prints this throwable and its backtrace to the print stream
         PrintStream ps = new PrintStream(out);      
         e.printStackTrace(ps);
      }
   }
  
   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;
   }
}

輸出

假設我們有一個文字檔案 file.txt,它作為我們示例程式的輸出生成。檔案內容包括 -

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

示例:列印 Exception 的堆疊跟蹤

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

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {

      OutputStream out;
      try {
         raiseException();
      } catch(Throwable e) {
         out = new FileOutputStream("file.text");
         // prints this throwable and its backtrace to the print stream
         PrintStream ps = new PrintStream(out);      
         e.printStackTrace(ps);
      }
   }
  
   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;
   }
}

輸出

假設我們有一個文字檔案 file.txt,它作為我們示例程式的輸出生成。檔案內容包括 -

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

示例:列印 RuntimeException 的堆疊跟蹤

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

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {

      OutputStream out;
      try {
         raiseException();
      } catch(Throwable e) {
         out = new FileOutputStream("file.text");
         // prints this throwable and its backtrace to the print stream
         PrintStream ps = new PrintStream(out);      
         e.printStackTrace(ps);
      }
   }
  
   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;
   }
}

輸出

假設我們有一個文字檔案 file.txt,它作為我們示例程式的輸出生成。檔案內容包括 -

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

© . All rights reserved.