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.