You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I am launching multiple coroutines using async, directly in scope, and calling .await() in another coroutine with launch builder.
All async are in scope level so calling await will throw an exception, so I wrapped deferred.await() with try-catch and expected that it should not cancel all other coroutines that are running.
But all coroutines are canceled. Please find the code.
privatefunmultipleCoroutinesAtScopeLevel() {
val scope =CoroutineScope(CoroutineName("ParentJob"))
val firstDef = scope.async(CoroutineName("firstDef")) {
delay(2000)
throwIllegalAccessException();
return@async "First"
}
val secondDef = scope.async(CoroutineName("secondDef")) {
delay(4000)
return@async "Second"
}
val thirdDeff = scope.async(CoroutineName("thirdDeff")) {
delay(3000)
return@async "third"
}
val helperJob = scope.launch(CoroutineName("HelperJob")) {
try {
firstDef.await()
} catch (e:Exception) {
Log.e(TAG, "Exception at $firstDef")
}
secondDef.await()
thirdDeff.await()
}
}
Output:
"secondDef#2":DeferredCoroutine{Cancelled}@9a4604f
"thirdDeff#3":DeferredCoroutine{Cancelled}@c6f43dc
Exception at "firstDef#1":DeferredCoroutine{Cancelling}@a351ce5
"HelperJob#4":StandaloneCoroutine{Cancelled}@58943ba
If this is as expected, may I know a little more about it?
The text was updated successfully, but these errors were encountered:
Understand. I think it would be better if we have detailed docs on exception handling in a nested coroutine and exception propagation to parent. It took quite some time to me understand after trying out some samples and reading some blogs.
Hi,
I am launching multiple coroutines using async, directly in scope, and calling .await() in another coroutine with launch builder.
All async are in scope level so calling await will throw an exception, so I wrapped deferred.await() with try-catch and expected that it should not cancel all other coroutines that are running.
But all coroutines are canceled. Please find the code.
Output:
"secondDef#2":DeferredCoroutine{Cancelled}@9a4604f
"thirdDeff#3":DeferredCoroutine{Cancelled}@c6f43dc
Exception at "firstDef#1":DeferredCoroutine{Cancelling}@a351ce5
"HelperJob#4":StandaloneCoroutine{Cancelled}@58943ba
If this is as expected, may I know a little more about it?
The text was updated successfully, but these errors were encountered: