Prateek Kumar
02/27/2024, 6:01 PMviewModelScope.launch(coroutineExceptionHandler) {
supervisorScope {
for (i in 0..10) {
launch {
delay(i * 1000L)
throw RuntimeException("abc")
}
}
}
}
Here in Exception Handler all 10 exceptions are recorded.
Approach 2:-
viewModelScope.launch(coroutineExceptionHandler+ SupervisorJob()) {
for (i in 0..10) {
launch {
delay(i * 1000L)
throw RuntimeException("def")
}
}
}
With this only one exception is recorded and rest are cancelled after first exception.
Why is 1 and 2 works differently? Shouldn’t the parent Job used to create child jobs?Sam
02/27/2024, 7:33 PMlaunch
always creates a new job as the child of the job in its starting context. Passing a job as an argument to the launch function merely replaces its parent job, and is rarely if ever a useful thing to do.Prateek Kumar
02/28/2024, 4:25 AM_viewModelScope_._launch_(coroutineExceptionHandler) *{*
supervisorScope *{*
for (i in 0..10) {
_launch_ *{*
delay(i * 1000L)
_launch_ *{*
//This scope should not be Supervisor Job but it is
throw RuntimeException("def")
}
}
}
}
}
Above code still collects all the exceptions even though the launch throwing the exception is not directly part of supervisorScope