取消Kotlin协程后无法捕获异常引发 [英] Can't catch exception throwing after Kotlin coroutine is cancelled

查看:387
本文介绍了取消Kotlin协程后无法捕获异常引发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 kotlinx.coroutines lib我无法捕获抛出的异常协程取消后.这会导致应用崩溃.

Using kotlinx.coroutines lib I can't catch an exception if it was thrown after coroutine is canceled. This leads to app crash.

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上运行此功能会产生以下日志输出

Running this functions on Android produces the following log output

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异常的情况下静默取消作业?

Seems to be calling cancel() throws CancellationException which is caught successfully. But the subsequent RuntimeException is not caught. I suppose the following exceptions after the job is canceled should be ignored by the lib? Or how I can cancel job silently without throwing a CancellationException exception?

推荐答案

使用

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

这篇关于取消Kotlin协程后无法捕获异常引发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆