Converting Java stack trace to string
If you have an Exception e, you might want to convert the stackTrace to a string to add to your logs. Here’s a quick code sample I found to do so from JavaPractices:
public static String getStackTrace(Throwable aThrowable) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
aThrowable.printStackTrace(printWriter);
return result.toString();
}
Advertisement
leave a comment