Java.lang.Throwable.printStackTrace() 方法
描述
java.lang.Throwable.printStackTrace() 方法将此 throwable 及其回溯打印到标准错误流。 它在错误输出流上打印此 Throwable 对象的堆栈跟踪,即字段 System.err 的值。
声明
以下是 java.lang.Throwable.printStackTrace() 方法的声明。
public void printStackTrace()
参数
NA
返回值
此方法不返回任何值。
异常
NA
示例
下面的例子展示了 java.lang.Throwable.printStackTrace() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class ThrowableDemo {
public static void main(String[] args) {
try {
ExceptionFunc();
} catch(Throwable e) {
// prints stacktace for this Throwable Object
e.printStackTrace();
}
}
public static void ExceptionFunc() 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)

