Im browsing a bit the code from Result.kt in stdli...
# codingconventions
g
Im browsing a bit the code from Result.kt in stdlib. Im wondering how come
runCatching()
does not catch
Exception
instead of
Throwable
? Shouldn't we always let errors such as
StackOverflow
be thrown ?
p
It’s probably worth reading the design document around the
kotlin.Result
class for a better understanding. https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/result.md
In short, the
Result
class was primarily added to support the
Continuation
callback interface (which needed to accept any failure to resume with) - it really should be avoided for use in “normal” code in favour of domain-specific types, and/or try/catch with explicit catches.
runCatching
catching Throwable is indeed something to be cautious of as it will inevitably trip you up in the future.