Kotlin学习笔记之 32 协程异常处理

《Kotlin学习笔记之 32 协程异常处理》

首发于公众号: DSGtalk1989

32.协程异常处理

  • 异常的传播

launchactor构建器是不传播异常的,asyncproduce是传播异常的。这里的传播说的更容易理解一点叫做,往外抛,即不传播异常就是在本协程中自己消化,异常发生在本协程所在的线程;传播异常表示不在本协程所在线程发生,异常直接往外抛到启动该协程所在的线程。我们看如下demo

val job = GlobalScope.launch {
    println("${Thread.currentThread().name}   :   Throwing exception from launch")
    throw IndexOutOfBoundsException()
}
job.join()
println("Joined failed job")
val deferred = GlobalScope.async {
    println("${Thread.currentThread().name}   :   Throwing exception from async")
    throw ArithmeticException()
}
deferred.await()
println("Unreached")

控制台打印

DefaultDispatcher-worker-1   :   Throwing exception from launch
Exception in thread "DefaultDispatcher-worker-1" java.lang.IndexOutOfBoundsException
    at salamanca.DestructTestKt$main$1$job$1.invokeSuspend(DestructTest.kt:52)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
Joined failed job
DefaultDispatcher-worker-1   :   Throwing exception from async
Exception in thread "main" java.lang.ArithmeticException
    at salamanca.DestructTestKt$main$1$deferred$1.invokeSuspend(DestructTest.kt:58)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)

我们看到两个异常分别在协程的线程和主线程中抛出来一个由于在协程内,所以不会造成主线程异常,一个在主线程,所以直接跪了。

  • CoroutineExceptionHandler

所以针对上面这种情况,我们应该怎么办,比如我们想要去catch一下,针对上面的launchawait方法,我们包裹了try catch如下

try{
    job.join()
}catch (e : Exception){
    println(e.toString())
}

try{
    deferred.await()
}catch (e : Exception){
    println(e.toString())
}

发现await是可以捕捉的,join无法用try catch捕捉。所以针对这种不传播异常的,我们应该怎么去捕捉它呢。CoroutineExceptionHandler登场。

我们将上面的代码作如下改动

val handler = CoroutineExceptionHandler { _, exception ->
    println("Caught $exception")
}
val job = GlobalScope.launch(handler) {
    println("${Thread.currentThread().name}   :   Throwing exception from launch")
    throw IndexOutOfBoundsException()
}
job.join()

打印如下,舒坦。

DefaultDispatcher-worker-1   :   Throwing exception from launch
Caught java.lang.IndexOutOfBoundsException

Kotlin学习笔记之 1 基础语法

Kotlin学习笔记之 2 基本数据类型

Kotlin学习笔记之 3 条件控制

Kotlin学习笔记之 4 循环控制

Kotlin学习笔记之 5 类和对象

Kotlin学习笔记之 6 继承

Kotlin学习笔记之 7 接口

Kotlin学习笔记之 8 扩展

Kotlin学习笔记之 9 数据类与密封类

Kotlin学习笔记之 10 泛型

Kotlin学习笔记之 11 枚举类

Kotlin学习笔记之 12 对象表达式和对象声明

Kotlin学习笔记之 13 基础操作符run、with、let、also、apply

Kotlin学习笔记之 14 包与导入

Kotlin学习笔记之 15 伴生对象

Kotlin学习笔记之 16 委托

Kotlin学习笔记之 17 可观察属性

Kotlin学习笔记之 18 函数

Kotlin学习笔记之 19 高阶函数与 lambda 表达式

Kotlin学习笔记之 20 内联函数

Kotlin学习笔记之 21 解构声明

Kotlin学习笔记之 22 集合

Kotlin学习笔记之 23 相等判断

Kotlin学习笔记之 24 操作符重载

Kotlin学习笔记之 25 异常捕捉

Kotlin学习笔记之 26 反射

Kotlin学习笔记之 27 类型别名

Kotlin学习笔记之 28 协程基础

Kotlin学习笔记之 29 上下文与调度器

Kotlin学习笔记之 30 协程取消与超时

Kotlin学习笔记之 31 协程挂起函数的组合

Kotlin学习笔记之 32 协程异常处理

Kotlin学习笔记之 33 协程 & Retrofit

    原文作者:super_shanks
    原文地址: https://www.jianshu.com/p/45d612299250
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞