相关疑难解决方法(0)

CoroutineExceptionHandler 在作为启动上下文提供时未执行

当我运行这个:

fun f() = runBlocking {
    val eh = CoroutineExceptionHandler { _, e -> trace("exception handler: $e") }
    val j1 = launch(eh) {
        trace("launched")
        delay(1000)
        throw RuntimeException("error!")
    }
    trace("joining")
    j1.join()
    trace("after join")
}
f()
Run Code Online (Sandbox Code Playgroud)

这是输出:

[main @coroutine#1]: joining
[main @coroutine#2]: launched
java.lang.RuntimeException: error!
    at ExceptionHandling$f9$1$j1$1.invokeSuspend(ExceptionHandling.kts:164)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
    at kotlinx.coroutines.ResumeModeKt.resumeMode(ResumeMode.kt:67)
Run Code Online (Sandbox Code Playgroud)

根据CoroutineExceptionHandler的文档,eh应该执行我提供的处理程序。但事实并非如此。这是为什么?

coroutine kotlin kotlinx.coroutines

6
推荐指数
1
解决办法
2721
查看次数

标签 统计

coroutine ×1

kotlin ×1

kotlinx.coroutines ×1