val xHandlerParent = CoroutineExceptionHandler { c, e ->
println("Parent Handled Crash")
}
val xHandlerLeaf = CoroutineExceptionHandler { c, e ->
println("Leaf Handled Crash")
}
fun main() {
CoroutineScope(xHandlerParent).launch(xHandlerLeaf) {
delay(1000)
throw Exception("Some Exception 1")
}
Thread.sleep(2000)
}
prints out
Leaf Handled Crash
.
However, I expected the output to be
Parent Handled Crash
, because an exception bubbles up to the top-most parent and
CoroutineExceptionHandler
of that top-most parent should be used.
If I replace
CoroutineScope(xHandlerParent). ...
with
runBlocking { ... }
, the
xHandlerLeaf
is not used, as I expected, because the top-most parent is used and that is a scoped-parent that just throws the exception up the call-stack.
I’m not sure if this is a bug or if I don’t understand how exceptions are handled 🙂