https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

PHondogo

10/19/2023, 3:33 PM
Just thoughts that it will be better to make Cancellation exception extends from Throwable. Normal exception handling will be with less code to write without checking case of Cancelation exception.
j

Joffrey

10/19/2023, 3:34 PM
Normal exception handling code shouldn't catch
CancellationException
, though 🤔 (it shouldn't catch
Exception
in general). I mean, assuming that "normal" means regular business code, not framework code (in framework code that would make sense, but then it's just one
ensureActive()
call in the catch block).
p

PHondogo

10/19/2023, 3:41 PM
By normal I mean not Error and CancellationException. I agree that catching base Exception is not tight way, but there are some cases. For examle logging or framework logic. Almost in every such case it is needed to filter out CancellatiinException.
j

Joffrey

10/19/2023, 3:46 PM
There should be only a limited number of places that handle "all errors" generally like this, and usually this is in framework code. If you need to "exclude CancellationException", you should use
ensureActive()
instead of 2 catch blocks.