JAVA: How to capture java exception stack trace in String

Many a times you might need to capture java exception (stack trace) in 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.

Comments

Popular posts from this blog

AEM as a Cloud Service (AEMaaCS) – Architecture Overview

AEM 6.3 - Bundle Whitelisting - Deprecation of administrative authentication

AEM - Query list of components and templates

Custom synchronisation or rollout action for blueprint ?