lesincs
07/02/2022, 5:02 AMasync {} throws exception not until .await() be called.lesincs
07/02/2022, 5:02 AMval scope = MainScope()
scope.launch {
val deferred = async<String> {
delay(200)
throw RuntimeException()
}
try {
val result = deferred.await()
print(result)
} catch (e: Exception) {
print("catch exception")
}
}
In this code snippet, the try catch is not gonna work, which confused me that isn’t async throwing exceptions only when await() is called, so the exception should be catched?louiscad
07/04/2022, 3:18 AMasync with a local coroutineScope { … }, and surround that block with the try catch you want.
Otherwise, you're breaking structured concurrency, which leads to the surprising behavior you've been witnessing.lesincs
07/04/2022, 11:30 AMsupervisorScope , and then it works as I am expecting, and I think if I use launch instead of async , I would like to take your suggestion, use coroutineScope and then try catch it, thanks again.😁louiscad
07/04/2022, 11:45 AMsupervisorScope works for both async and launch FYI.