How can I convert a stack trace to a string?

Throwable.getStackTrace()的结果转换为描述堆栈跟踪的字符串的最简单方法是什么?


One can use the following method to convert an Exception stack trace to String . This class is available in Apache commons-lang which is most common dependent library with many popular open sources

org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable)


使用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);

这应该工作:

StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
链接地址: http://www.djcxy.com/p/14466.html

上一篇: 如何在Node.js中打印堆栈跟踪?

下一篇: 如何将堆栈跟踪转换为字符串?