将Throwable.getStackTrace()
的结果转换为描述堆栈跟踪的字符串的最简单方法是什么?
Use Throwable.printStackTrace(PrintWriter pw)
to send the stack trace to an appropriate writer.
(使用Throwable.printStackTrace(PrintWriter pw)
将堆栈跟踪发送到适当的编写器。)
import java.io.StringWriter;
import java.io.PrintWriter;
// ...
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String sStackTrace = sw.toString(); // stack trace as a string
System.out.println(sStackTrace);