April 11, 2020
Estimated Post Reading Time ~

How to capture java exception stack trace in String

Many a times you might need to capture java exception (stack trace) in the string, here is a small code snippet to do that:
StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); throwable.printStackTrace(pw); String stackTrace = sw.getBuffer().toString();

This same method can be used to capture string from other streams and assign it to a String/StringBuffer.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.