How do I capture an exception stack trace and put it into a string?

Luigi Viggiano

Here's how to print the trace in a string:

Exception ex = new Exception("something went wrong");
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
System.out.println("stacktrace = " + stacktrace);
HTH.
Luigi
About | Sitemap | Contact