Hi, in Java, it is considered bad practice for try...
# announcements
k
Hi, in Java, it is considered bad practice for try catching
Throwable
instead of `Exception`Does anyone know why
runCatching
for
kotlin.Result
is catching
Throwable
instead of
Exception
? https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/run-catching.html
e
Frankly, I never encountered this kind of distinction. I’d say catching either
Throwable
or
Exception
is something to be avoided in general code. If you have to catch, then it should be limited to the most specific type.
runCatching
on the other side, has completely different use-cases well documented here: https://github.com/Kotlin/KEEP/blob/master/proposals/stdlib/result.md
🙏 1
In my practive as Java programmer we’ve always flagged catching
Exception
instead of
Throwable
as a mistake novice programmers make in those rare cases when catching everything was indeed called for by the business logic of the code (some top-level code that has to log all exceptions due to counter-inituituive naming has to actually catch all
Throwable
types)
k
That make sense. Thanks for the explanation.