Bootstrap

Kotlin Coroutines Exception

Kotlin Coroutines Exception

官方文档:https://www.kotlincn.net/docs/reference/coroutines/exception-handling.html

kotlin coroutines的exception有几种容易混淆的处理情形,在这里列举一下:

withContext

GlobalScope.launch(Dispatchers.Main) {
   
    try {
   
        withContext(Dispatchers.IO) {
   
            Log.d("coroutine exception", "start to throw")
            throw java.lang.Exception("hahaha")
        }
    } catch (e : java.lang.Exception){
   
        Log.d("coroutine exception", "outside "+e.message)
    }
}

运行结果:

coroutine exception: start throw
coroutine exception: outside hahaha

Async

GlobalScope.
;