Skip to content

Instantly share code, notes, and snippets.

@KenjiOhtsuka
Last active May 20, 2018 15:12
Show Gist options
  • Save KenjiOhtsuka/3d728ebd4c0df8bed29756826753982c to your computer and use it in GitHub Desktop.
Save KenjiOhtsuka/3d728ebd4c0df8bed29756826753982c to your computer and use it in GitHub Desktop.
How to Print Stack Trace at Any Point in Kotlin
/*
* Throwable, StackTraceElement is in java.lang
*
* StackTraceElement has the following attributes (get methods)
* className
* methodName
* fileName
* lineNumber
*/
fun printStackTrace() {
Throwable().stackTrace.forEach {
println(it.className + "#" + it.methodName + "(" + it.fileName + ":" + it.lineNumber + ")")
}
}
fun main(args: Array<String>) {
printStackTrace()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment