Stack Trace to String in Java

import java.io.PrintWriter;
import java.io.StringWriter;
 
public class StackTraceToStringExample {
       
        public static void main(String args[]){
               
                try{
                        Integer.parseInt("Not a number");
                       
                }catch(NumberFormatException e){
                        StringWriter sWriter = new StringWriter();
                        PrintWriter pWriter = new PrintWriter(sWriter);
                        e.printStackTrace(pWriter);
                        String strStackTrace = sWriter.toString();
                       
                        System.out.println("Stacktrace converted to String: " + strStackTrace);
                }
        }
       
}