When should I use `val scope = SupervisorScope()` ...
# coroutines
d
When should I use
val scope = SupervisorScope()
over
val scope = CoroutineScope(CoroutineExceptionHandler { ctx, e -> })
?
s
When an exception is thrown but not caught by a child-Coroutine in a scope with a supervisor job (SupervisorScope), its other child Coroutines keep running and the scope remains active. Using a 'regular' scope with a CEH or not, the scope its Job and all its child-Coroutines will finish when one of its child-Coroutines throws but not catch an exception.
“Exceptional Exceptions for Coroutines made easy…? part II: Supervision” by Anton Spaans https://link.medium.com/V0VsGfpEv0
Providing a CEH that does nothing, does not prevent the scope from being canceled. A CEH will receive the exception instead of the uncaught-exception-handler, possibly preventing your app from crashing
d
Yeah I thought as much.