void printStackTrace(PrintStream s)
描述 (Description)
java.lang.Throwable.printStackTrace(PrintStream s)方法将此throwable及其backtrace打印到指定的打印流。
声明 (Declaration)
以下是java.lang.Throwable.printStackTrace()方法的声明
public void printStackTrace(PrintStream s)
参数 (Parameters)
s - 这是用于输出的PrintStream
返回值 (Return Value)
此方法不返回任何值。
异常 (Exception)
NA
例子 (Example)
以下示例显示了java.lang.Throwable.printStackTrace()方法的用法。
package com.iowiki;
import java.lang.*;
public class ThrowableDemo {
public static void main(String[] args) throws Throwable {
OutputStream out;
try {
ExceptionFunc();
} 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 ExceptionFunc() 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)