How can I convert a stack trace to a string?

前端 未结 30 1591
再見小時候
再見小時候 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条回答
  •  盖世英雄少女心
    2020-11-22 15:06

    Kotlin

    Extending the Throwable class will give you the String property error.stackTraceString:

    val Throwable.stackTraceString: String
      get() {
        val sw = StringWriter()
        val pw = PrintWriter(sw)
        this.printStackTrace(pw)
        return sw.toString()
      }
    

提交回复
热议问题