如何获取e.printStackTrace()
并将其存储到String
变量中?
我想在我的程序中稍后使用e.printStackTrace()
生成的字符串。
我还是Java的新手,所以我不太熟悉我认为的StringWriter
将是解决方案。或者,如果您有任何其他想法,请告诉我。感谢
答案 0 :(得分:420)
的内容
StringWriter errors = new StringWriter();
ex.printStackTrace(new PrintWriter(errors));
return errors.toString();
应该是你需要的。
相关文件:
答案 1 :(得分:70)
Guava通过Throwables.getStackTraceAsString(Throwable):
轻松完成Exception e = ...
String stackTrace = Throwables.getStackTraceAsString(e);
在内部,这就是@Zach L建议的。
答案 2 :(得分:14)
与Guava一样,Apache Commons Lang在ExceptionUtils.getFullStackTrace
中有org.apache.commons.lang.exception
。 StackOverflow上的From a prior answer。
答案 3 :(得分:13)
您必须使用getStackTrace ()
方法而不是printStackTrace()
。这是一个good example:
import java.io.*;
/**
* Simple utilities to return the stack trace of an
* exception as a String.
*/
public final class StackTraceUtil {
public static String getStackTrace(Throwable aThrowable) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
aThrowable.printStackTrace(printWriter);
return result.toString();
}
/**
* Defines a custom format for the stack trace as String.
*/
public static String getCustomStackTrace(Throwable aThrowable) {
//add the class name and any message passed to constructor
final StringBuilder result = new StringBuilder( "BOO-BOO: " );
result.append(aThrowable.toString());
final String NEW_LINE = System.getProperty("line.separator");
result.append(NEW_LINE);
//add each element of the stack trace
for (StackTraceElement element : aThrowable.getStackTrace() ){
result.append( element );
result.append( NEW_LINE );
}
return result.toString();
}
/** Demonstrate output. */
public static void main (String... aArguments){
final Throwable throwable = new IllegalArgumentException("Blah");
System.out.println( getStackTrace(throwable) );
System.out.println( getCustomStackTrace(throwable) );
}
}
答案 4 :(得分:13)
您可以使用Apache Commons 3 class ExceptionUtils.getStackTrace(Throwable t);
中的org.apache.commons.lang3.exception.ExceptionUtils
。
http://commons.apache.org/proper/commons-lang/
ExceptionUtils.getStackTrace(Throwable t)
代码示例:
try {
// your code here
} catch(Exception e) {
String s = ExceptionUtils.getStackTrace(e);
}
答案 5 :(得分:6)
StackTraceElement[] stack = new Exception().getStackTrace();
String theTrace = "";
for(StackTraceElement line : stack)
{
theTrace += line.toString();
}
答案 6 :(得分:2)
使用apache commons-lang3 lib
import org.apache.commons.lang3.exception.ExceptionUtils;
//...
String[] ss = ExceptionUtils.getRootCauseStackTrace(e);
logger.error(StringUtils.join(ss, System.lineSeparator()));
答案 7 :(得分:1)
call: getStackTraceAsString(sqlEx)
public String getStackTraceAsString(Exception exc)
{
String stackTrace = "*** Error in getStackTraceAsString()";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream( baos );
exc.printStackTrace(ps);
try {
stackTrace = baos.toString( "UTF8" ); // charsetName e.g. ISO-8859-1
}
catch( UnsupportedEncodingException ex )
{
Logger.getLogger(sss.class.getName()).log(Level.SEVERE, null, ex);
}
ps.close();
try {
baos.close();
}
catch( IOException ex )
{
Logger.getLogger(sss.class.getName()).log(Level.SEVERE, null, ex);
}
return stackTrace;
}