koufa
02/01/2019, 3:46 PMfun main() = runBlocking {
try {
cScope()
} catch (error: Throwable) {
println(error)
}
}
suspend fun cScope() = coroutineScope {
val def = async { failure() }
try {
def.await()
} catch (error: Throwable) {
println(error)
}
}
Why is it only possible to catch the error when wrapping cScope() and not def.await() ?Allan Wang
02/01/2019, 3:52 PMkoufa
02/01/2019, 3:54 PMsupervisionScope . But how can I handle this on Android using a ViewModel and implementing CoroutineScope . How can I there handle any exceptions without cancelling my scope forever?Allan Wang
02/01/2019, 3:57 PMkoufa
02/01/2019, 4:00 PMsupervisorScope unhandled exceptions will affect other children. So if I change coroutineScope to it in my example and I remove the try/catch in the fun cScope then other children would be cancelled.koufa
02/01/2019, 4:01 PMCoroutineScope and I use async and the async throws? How can I catch the error? Will my scope then be cancelled forever?Allan Wang
02/01/2019, 4:01 PMkoufa
02/01/2019, 4:02 PMsuspend fun cScope() = supervisorScope {
val def = async { failure() }
def.await()
suspendingWork()
}koufa
02/01/2019, 4:03 PMtry/catchkoufa
02/01/2019, 4:03 PMsupervisorScope. If I would use coroutineScope then it would not matter if I had used try/catch or notAllan Wang
02/01/2019, 4:04 PMgildor
02/02/2019, 11:15 AMgildor
02/02/2019, 11:16 AMBut what will happen on Android if my component implementsYes, it will be cancelled this is why for UI use cases SuervisorJob is better choice than Joband I use async and the async throws? How can I catch the error? Will my scope then be cancelled forever?CoroutineScope