Hello, I’ve the following scope created in my fragment;
private val scope = CoroutineScope(Dispatchers.Default + SupervisorJob())
...
val handler = CoroutineExceptionHandler { _, exception ->
println("TAG-Caught $exception")
}
scope.launch(handler) {
launch {
println("TAG-first job is running")
delay(200)
}
// second job
testParentChildWithExceptionWithSupervision()
launch {
println("TAG-third job is running")
}
}
then the function testParentChildWithExceptionWithSupervision
looks like;
suspend fun testParentChildWithExceptionWithSupervision() {
supervisorScope {
val job1 = launch {
println("TAG-running first (inner) child")
delay(200)
throw ArithmeticException()
}
val job2 = launch {
job1.join()
println("TAG-First child is cancelled: ${job1.isCancelled}, but second one is still active")
delay(200)
}
job1.join()
println("TAG-second child is cancelled: ${job2.isCancelled}")
println("TAG-ENDING")
}
}
The output is like;
The point that I don’t quite get is about a part in the docs seems like doesn’t fit with what’s being executed;
Another crucial difference between regular and supervisor jobs is exception handling. Every child should handle its exceptions by itself via exception handling mechanisms. This difference comes from the fact that child’s failure is not propagated to the parent.
I see that the exception is still propagated upwards. Shouldn’t it be handled in the method contains the supervisorScope
and not be propagated back to the fragment?