Java Convert Exception StackTrace as String

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.


public static String getExceptionStackTraceAsString(Exception exception) {
  StringWriter sw = new StringWriter();
  exception.printStackTrace(new PrintWriter(sw));
  return sw.toString();

Related Posts Plugin for WordPress, Blogger...


Leave a Reply

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