简体   繁体   English

如何从没有printStackTrace的异常中获取stackTrace?

[英]How to get the stackTrace from an exception without printStackTrace?

Is it possible to get the whole trace from an exception without using printStackTrace() ?. 不使用printStackTrace()是否有可能从异常中获取整个跟踪? One of the reasons it's because I use CheckStyle and I'm getting an ugly message. 原因之一是因为我使用CheckStyle并收到一条难看的消息。 It's not enough just to call toString() . 仅调用toString()不够的。 I want to get the complete message, because I needed to send me by email. 我想获取完整的消息,因为我需要通过电子邮件发送给我。

Currently, I'm doing 目前,我正在做

 StringWriter e = new StringWriter();
 exception.printStackTrace(new PrintWriter(e));

--Avoid tracing with printStackTrace() , use log4j instead. -避免使用printStackTrace()跟踪,而改用log4j。

Otherwise, I could disable CheckStyle there. 否则,我可以在那里禁用CheckStyle

If you want to get error details try Apache Commons Lang api 如果要获取错误详细信息,请尝试使用Apache Commons Lang api

There is a class ExceptionUtils . 有一个ExceptionUtils类。 You can use getFullStackTrace 您可以使用getFullStackTrace

Have you tried?: 你有没有尝试过?:

Throwable thr = e.getStackTrace();
StackTraceElement[] lines = thr.getStackTrace();

And working from there. 从那里开始工作。 Also, take a look at the javadocs Oli linked above 另外,看看上面链接javadocs Oli

您可以从当前线程获取堆栈跟踪,请检查以下内容:

Thread.currentThread().getStackTrace()

Try this, you will be able to print stack trace with no check style error. 尝试此操作,您将能够在没有检查样式错误的情况下打印堆栈跟踪。

import org.apache.commons.lang.exception.ExceptionUtils;

String errorMessage = ExceptionUtils.getFullStackTrace(exception);  
logger.debug(errorMessage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM