How can I get the e.printStackTrace()
and store it into a String
variable?
I want to use the string generated by e.printStackTrace()
later in my program.
I'm still new to Java so I'm not too familiar with StringWriter
that I think
will be the solution. Or if you have any other ideas please let me know. Thanks
Along the lines of Guava, Apache Commons Lang has
ExceptionUtils.getFullStackTrace
inorg.apache.commons.lang.exception
. From a prior answer on StackOverflow.Use the apache commons-lang3 lib
You can use the
ExceptionUtils.getStackTrace(Throwable t);
from Apache Commons 3 classorg.apache.commons.lang3.exception.ExceptionUtils
.http://commons.apache.org/proper/commons-lang/
ExceptionUtils.getStackTrace(Throwable t)
Code example:
Something along the lines of
Ought to be what you need.
Relevant documentation:
You have to use
getStackTrace ()
method instead ofprintStackTrace()
. Here is a good example: