Paul Woitaschek
06/05/2019, 11:33 AMimport kotlinx.coroutines.*
val job: Job = Job()
val scope = CoroutineScope(Dispatchers.Default + job)
fun throwsAsync(): Deferred<Unit> = scope.async {
throw Exception()
}
fun loadData() = scope.launch {
throwsAsync().await()
}
suspend fun main() {
loadData().join()
scope.launch { println("worked") }.join()
joinAll()
println("done")
}
Why doesn't this crash?marstran
06/05/2019, 11:45 AMlaunch
is a fire&forget type of coroutine builder. The caller is not expected to handle errors. You need to register a CoroutineExceptionHandler
on it: https://kotlinlang.org/docs/reference/coroutines/exception-handling.html#coroutineexceptionhandlerasync().await()
however, will throw when you do await()
.