Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correctly printstacktrace of servlet exception

so i am using a filter to catch servlet exception (because we are using a mix of jsf/plain servlets)

when catching the ServletException and calling printstacktrace most of the information is lost.

the "true" root exception seems to be hidden behind the "funny" expression

((ServletException) e.getRootCause().getCause()).getRootCause().getCause().getCause().getCause()

this is clearly not the way to do it.

is the an easy way to print the "full" information of such an exception. can someone explain me why the exception is wrapped this way?

like image 874
Andreas Petersson Avatar asked Dec 30 '22 23:12

Andreas Petersson


2 Answers

Take a look at the ExceptionUtils class from commons-lang. It contains several useful methods for printing the entire chain of exceptions.

like image 107
David Avatar answered Jan 03 '23 07:01

David


after i had a look at ExceptionUtils, this solved the problem!

    final StringWriter stacktrace = new StringWriter();
    ExceptionUtils.printRootCauseStackTrace(throwable,new PrintWriter(stacktrace));
    msg.append(stacktrace.getBuffer());

this prints out the full stacktrace with every piece of information that is relevant.

like image 25
Andreas Petersson Avatar answered Jan 03 '23 06:01

Andreas Petersson