Convert Stack Trace to String in JAVA

4

What would be the best way to convert Stack Trace from Exception to String ?

    
asked by anonymous 08.11.2016 / 14:00

1 answer

5

I came across this need and doing a web search I found this great solution in SOen, which allows you to get this important information as String , without having to import other libraries:

try {
} catch (Exception e) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    sw.toString(); //Aqui obtenho a String
}
    
08.11.2016 / 14:00