Monday, January 25, 2010

How to convert Exception Stack Trace to String in Java ?

Below code snippet shows how to convert a Exception Stack Trace to String object in Java

public static String convertExStackTraceToString(Exception ex)

{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
sw.flush();
String strStackTrace = sw.toString();
try
{
sw.close();
pw.close();
}catch (IOException ex1)
{
ex1.printStackTrace();
}
return strStackTrace;
}

No comments:

Post a Comment