How can I convert a stack trace to a string?

前端 未结 30 1443
再見小時候
再見小時候 2020-11-22 14:51

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

30条回答
  •  萌比男神i
    2020-11-22 15:22

    With Java 8 Stream API you can do something like this:

    Stream
        .of(throwable.getStackTrace())
        .map(StackTraceElement::toString)
        .collect(Collectors.joining("\n"));
    

    It will take array of stack trace elements, convert them to string and join into multiline string.

提交回复
热议问题