Java Convert Exception StackTrace as String
Posted by tech on
April 8, 2008
Remember that exception.printStackTrace() method that outputs detailed messages about the error in the console? That method returns void so there’s no way to get those messages stored in a String. Try this handy method does the trick. Just pass the exception object as the parameter for this method.
import java.io.PrintWriter;
import java.io.StringWriter;
public static String getExceptionStackTraceAsString(Exception exception) {
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
return sw.toString();
}











November 19th, 2009 at 1:52 am
Very useful content in there.. Well done..
November 5th, 2011 at 2:02 am
Thankyou so much