打印 Java 中的堆栈跟踪
为了打印 Java 中的堆栈跟踪,我们使用 java.lang.Throwable.printStackTrace() 方法。该 printStackTrace() 方法会将该 throwable 及其回溯打印到标准错误流中。
声明 - java.lang.Throwable.printStackTrace() 方法的声明如下 -
public void printStackTrace()
让我们看一个打印 Java 中堆栈跟踪的程序。
示例
public class Example {
public static void main(String args[]) throws Throwable {
try {
int n = 3;
System.out.println(n/0);
} catch (ArithmeticException e) {
System.out.println("Printing stack trace...");
e.printStackTrace(); // prints the stack trace
}
}
}输出
Printing stack trace... java.lang.ArithmeticException: / by zero at Example.main(Example.java:8)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP