gpt4 book ai didi

android - 取消 Kotlin 协程后无法捕获异常抛出

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:33 25 4
gpt4 key购买 nike

使用 kotlinx.coroutines lib 取消协程后抛出的异常我是抓不到的。这会导致应用程序崩溃。

fun foo() {
val job = launch(UI) {
try {
Log.d("TAG", "Start coroutine")
run(CommonPool) {
Log.d("TAG", "Start bg task")
// Intentionally make bg task running for a long time
SystemClock.sleep(2000)
Log.d("TAG", "Throw bg task exception")
throw RuntimeException("Bg task exception")
}
} catch (e: Exception) {
Log.e("TAG", "Handle coroutine exception", e)
}
}

launch(UI) {
delay(1000)
Log.d("TAG", "Cancel job = ${job.cancel()}")
}

在 Android 上运行此函数会产生以下日志输出

07-26 15:09:10.038 31518-31518/co.foo.bar D/MainActivity: Start coroutine
07-26 15:09:10.044 31518-31547/co.foo.bar D/MainActivity: Start bg task
07-26 15:09:11.046 31518-31518/co.foo.bar D/MainActivity: Cancel job = true
07-26 15:09:11.047 31518-31518/co.foo.bar E/MainActivity: Handled coroutine exception
java.util.concurrent.CancellationException: Job was cancelled
at kotlinx.coroutines.experimental.JobSupport$CompletedExceptionally.getException(Job.kt:921)
at kotlinx.coroutines.experimental.RunCompletion.afterCompletion(Builders.kt:198)
...
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
07-26 15:09:12.046 31518-31547/co.foo.bar D/MainActivity: Throwing bg task exception

--------- beginning of crash
07-26 15:09:12.046 31518-31547/co.foo.bar E/AndroidRuntime: FATAL EXCEPTION: ForkJoinPool.commonPool-worker-1
Process: co.foo.bar, PID: 31518
java.lang.RuntimeException: Bg task exception
at co.foo.barsample.MainActivity$onCreate$1$job$1$1.doResume(MainActivity.kt:36)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:54)
at kotlinx.coroutines.experimental.DispatchTask.run(CoroutineDispatcher.kt:120)
at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1383)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:256)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1123)
at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1961)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1909)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:128)
07-26 15:09:12.050 1705-2190/system_process W/ActivityManager: Force finishing activity co.foo.bar/co.foo.barsample.MainActivity

似乎是在调用 cancel() 抛出 CancellationException 并被成功捕获。但是后续的RuntimeException没有被捕获。我想 lib 应该忽略作业取消后的以下异常吗?或者我如何在不抛出 CancellationException 异常的情况下静默取消作业?

最佳答案

使用CoroutineExceptionHandler作为处理异常的附加协程上下文,用于 launchrun like

run(CommonPool + CoroutineExceptionHandler({ _, e ->
Log.e("TAG", "CoroutineExceptionHandler", e)
})) {
...
}

launch(UI + CoroutineExceptionHandler({ _, e ->
Log.e("TAG", "CoroutineExceptionHandler", e)
})) {
...
}

关于android - 取消 Kotlin 协程后无法捕获异常抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45326818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com