is there any way to get a more... usable stacktrac...
# announcements
s
is there any way to get a more... usable stacktrace other than
Throwable().printStackTrace()
as im trying to find what line in the function my function is throwing from
t
This information is in the stack trace
s
how would i aquire it?
t
What do you mean? What’s your context?
s
for example
Copy code
fun abort(e: String = "Aborted"): Nothing {
    println("Aborting with error: $e")
    println("stack trace:")
    Throwable().printStackTrace()
    throw Exception(e)
}

fun main() {
    a()
}

fun a() {
    println("aborting from a:2 from SampleLinux.kt:20")
    abort()
}
output:
Copy code
aborting from a:2 from SampleLinux.kt:20
Aborting with error: Aborted
stack trace:
kotlin.Throwable
        at kfun:sample.abort$default(kotlin.String;kotlin.Int)kotlin.Nothing (0x21a208)
        at Konan_start (0x21a182)
        at Konan_run_start (0x21a0f3)
        at Konan_main (0x21a047)
        at __libc_start_main (0x7f305064c223)
        at  (0x210029)
        at  ((nil))
Uncaught Kotlin exception: kotlin.Exception: Aborted
        at kfun:sample.abort(kotlin.String)kotlin.Nothing (0x21a490)
        at kfun:sample.abort$default(kotlin.String;kotlin.Int)kotlin.Nothing (0x21a208)
        at Konan_start (0x21a182)
        at Konan_run_start (0x21a0f3)
        at Konan_main (0x21a047)
        at __libc_start_main (0x7f305064c223)
        at  (0x210029)
        at  ((nil))
t
Is that kotlin native?
s
yes
t
you should probably ask that in the #C3SGXARS6 channel then
❤️ 2
s
ok
m
Comment on code if you’re worried about performance. You’re creating the stacktrace twice in your existing code because you create a Throwable, and an Exception.
Copy code
val e = Exception(message)
e.printStackTrace()
throw e
or
Copy code
throw Exception(message).also {
   it.printStackTrace()
}
s
ok? and im only interested in obtaining a backtrace
m
Just an unasked for code review/comment. Creating a stacktrace can be expensive, and your original code was creating it twice. That’s all my point was. Don’t know of a cleaner approach to creating it, so nothing useful specific to your question.
t
Are you only interested in generating a stack trace, or do you want to throw it too? Your code indicates you also want to throw it. If you just want the stack trace you could do it directly:
Copy code
`Thread.currentThread().stackTrace
s
is a backtrace and a stacktrace the same thing?
@tddmonkey Unresolved reference: Thread