공부

[Java] e.printStackTrace -> String

승가비 2022. 8. 10. 02:35
728x90
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();

https://stackoverflow.com/questions/1149703/how-can-i-convert-a-stack-trace-to-a-string

 

How can I convert a stack trace to a string?

What is the easiest way to convert the result of Throwable.getStackTrace() to a string that depicts the stacktrace?

stackoverflow.com


fun Exception.stackTrace(): String {
    val sw = StringWriter()
    this.printStackTrace(PrintWriter(sw))

    return sw.toString()
}
728x90