Java Convert Exception StackTrace as String
Posted by blogmeister 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();
}










2 Responses to “Java Convert Exception StackTrace as String”
Very useful content in there.. Well done..
By herc on Nov 19, 2009
Thankyou so much
By Muthu on Nov 5, 2011