Domanda

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.

È stato utile?

Soluzione

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

There is a class ExceptionUtils . You can use getFullStackTrace

Altri suggerimenti

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

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

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top