我一直在解决使用协同程序进行错误处理的问题,我已通过以下步骤缩小到此单元测试:
这一切都很好.但是,当我尝试使用相同的协同程序范围来启动新的协程时,这总是异常地完成同样的异常.
这是测试:
fun `when you throw an exception in a coroutine scope, is the coroutine scope dead?`() {
val parentJob = Job()
val coroutineScope = CoroutineScope(parentJob + Dispatchers.Default)
val deferredResult = coroutineScope.async { throw IllegalStateException() }
runBlocking {
try {
deferredResult.await()
} catch (e: IllegalStateException) {
println("We caught the exception. Good.")
}
try {
coroutineScope.async { println("we can still use the scope") }.await()
} catch (e: IllegalStateException) {
println("Why is this same exception still being thrown?") …
Run Code Online (Sandbox Code Playgroud)