Customized Exception Stack Trace in Java

Oracle Java

As a programmer, you must be familiar with Java stack trace, isn’t it? If you’re not familiar with exception stack trace, you can learn on how to produce and read the stack trace before you continue to read this article.

Converting a stack trace into a String is done with Throwable.printStackTrace(). If desired, a customized stack trace String can be defined by using the StackTraceElement class, as shown in the sample below:

If you run above sample code, you will see below output:

Please take note that above output is similar to Throwable.printStackTrace() output, but you can customize it on your own.

You also can use the ExceptionUtils.getStackTrace(Throwable t); from Apache Commons Lang class org.apache.commons.lang3.exception.ExceptionUtils.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*