I am running a coroutine in a SupervisorJob with a try/catch block surrounding only the await calls. The exception from the async block gets caught by the try/catch, but it still gets propagated and the app crashes.
This is what I have:
CoroutineScope(Dispatchers.IO + SupervisorJob()).launch {
val a = async {
delay(500)
throw Exception("excep a")
2
}
val b = async {
delay(500)
3
}
try {
println(a.await() + b.await())
} catch (e: Exception) {
println("exception: ${e.message}")
}
}
This is what I get (note that "excep a" gets caught):
exception: excep a
Exception in thread "DefaultDispatcher-worker-3 @coroutine#1" java.lang.Exception: excep a
at com.example.app.AuthTest$co2$1$a$1.invokeSuspend(AuthTest.kt:314)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)