Everything you need to know about the exception ha...
# feed
k
Everything you need to know about the exception handling mechanism in Kotlin Coroutines. Written by @marcinmoskala https://kt.academy/article/cc-exception-handling
👍 3
v
The most important thing to understand is, in this code:
Copy code
scopeA.launch(SupervisorJob()){ // coroutineX
    launch {
      // coroutineY
    }
    launch {
      // coroutineZ
    }
}
the result will be:
Copy code
[scopeA+SupervisorJob()]
  - [ScopeA+Job()] runs coroutineX
     - [ScopeA+Job()] runs coroutineY
     - [ScopeA+Job()] runs coroutineZ
So an exception in
coroutineY
will propagate up, and cancels
coroutineZ
also. The
Job
or
CoroutineContext
passed to the
launch
actually gets added to the receiver scope of the
launch
call, and the launched coroutine actually creates a new scope and its own Job. It gets a little bit to wrap the head around it 🤯 This is an excellent article by Roman E: https://elizarov.medium.com/coroutine-context-and-scope-c8b255d59055