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

martmists

12/08/2023, 1:48 PM
Should I be using
kotlinx.coroutines.CancellationException
or
kotlin.coroutines.cancellation.CancellationException
? on JVM they're both linked to the same thing, but I don't know about other platforms and/or what the most "future-proof" version is.
j

Joffrey

12/08/2023, 1:52 PM
On JS, WASM, and Native targets,
kotlinx.coroutines.CancellationException
is an alias of
kotlin.coroutines.cancellation.CancellationException
. You can see it in their KDocs: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-cancellation-exception/
m

martmists

12/08/2023, 1:54 PM
Hmm, then isn't it worth considering deprecating, since it's always equal to
kotlin.coroutines.cancellation.CancellationException
? I feel like having it duplicated might lead to confusion
j

Joffrey

12/08/2023, 2:03 PM
I'm not sure why it's this way, as I'm not part of the Kotlin team myself. I see that the coroutines library created some subclasses and maybe wanted the whole hierarchy to be in the same package. Usually the
kotlin.coroutines
package from the stdlib (not
kotlinX
) contains the primitives for the core language support of coroutines, so I'm not sure why they needed to represent cancellation there. I guess if you're using the kotlinx.coroutines library it makes sense to use the kotlinx
CancellationException
. I would also be happy to understand more if someone knows 🙂
Maybe @Dmitry Khalanskiy [JB] knows more? 🙏 (sorry to summon you specifically)
d

Dmitry Khalanskiy [JB]

12/08/2023, 2:10 PM
CancellationException
is useful often enough that adding a separate
kotlin.coroutines.cancellation
import just for it would cumulatively add a lot of useless noise.
j

Joffrey

12/08/2023, 2:11 PM
And I suppose the stdlib does need a cancellation exception too? I thought cancellation was only introduced at the coroutines library level, but I might have missed it in the language primitives
d

Dmitry Khalanskiy [JB]

12/08/2023, 2:17 PM
No, I don't think stdlib uses it anywhere. The Kotlin compiler knows about
CancellationException
because it has to highlight the definitions exported to Objective-C as throwing a
CancellationException
. Otherwise, Objective-C thinks of such exceptions as unexpected failures and crashes.
mind blown 3
today i learned 3
2 Views