Friday, 12 July 2013

How to get complete exception trace as a String ?

Cross posted from http://santoshsarmajv.blogspot.in/

By using below method you can get complete exception stack trace as a String.


public static String stackTraceToString(Throwable e) 
{
  String retValue = null;
  StringWriter sw = null;
  PrintWriter pw = null;
  try {
                  sw = new StringWriter();
                  pw = new PrintWriter(sw);
                  e.printStackTrace(pw);
                retValue = sw.toString();
  } 
               finally 
               {
           try {
                       if(pw != null) { pw.close();}
                       if(sw != null) { sw.close();}
               } 
                       catch (IOException ignore) {
    ignore.printStackTrace();
             }
      }
 return retValue+" \n ";
}

No comments:

Post a Comment