Alex Kuznetsov
05/06/2025, 12:44 PMrunCatching
catches a Throwable
not an Exception
? Even official documentation states that
• The Error
subclass represents serious fundamental problems that an application might not be able to recover from by itself. These are problems that you generally would not attempt to handle, such as OutOfMemoryError
or StackOverflowError
.
https://kotlinlang.org/docs/exceptions.html#exception-hierarchy
Personally we use a slightly modified version of runCatching
that only catches an Exception
. What am I missing?Joffrey
05/06/2025, 12:46 PMrunCatching
should just not be used in business code. It's bad to catch Throwable
, and almost as bad to catch all `Exception`s. The Result
type wasn't meant for business error handling, but only for conveying errors in a different way in infrastructure code in frameworks that don't relay exceptions via the original exception mechanism (like coroutines). If you need that, you probably also will deal with Throwable
.
(Here is a related SO answer I wrote some time ago: https://stackoverflow.com/questions/70847513/when-and-how-to-use-result-in-kotlin)