Guille
Guille

Reputation: 2320

How to get the stackTrace from an exception without printStackTrace?

Is it possible to get the whole trace from an exception without using printStackTrace()?. One of the reasons it's because I use CheckStyle and I'm getting an ugly message. It's not enough just to call 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.

Otherwise, I could disable CheckStyle there.

Upvotes: 1

Views: 1232

Answers (4)

Raj Pandiri
Raj Pandiri

Reputation: 111

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

Upvotes: 0

Salah
Salah

Reputation: 8657

You can get the stack trace from the current thread, check this :

Thread.currentThread().getStackTrace()

Upvotes: 1

Makky
Makky

Reputation: 17461

If you want to get error details try Apache Commons Lang api

There is a class ExceptionUtils . You can use getFullStackTrace

Upvotes: 3

Rogue
Rogue

Reputation: 11483

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

Upvotes: 2

Related Questions