设为首页 加入收藏

TOP

kotlin协程异常处理之-try catch(一)
2023-08-26 21:10:21 】 浏览:207
Tags:kotlin 程异常 -try catch

一、try catch

try catch是否一定有效呢?未必,来看一下:

1、withContext

import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        println("launch start")
        try {
            withContext(Dispatchers.IO) {
                // 可能抛出异常
            }
        } catch (ex: Exception) {
            println("withContext caught: ${ex.message}")
        }
        println("launch end")
    }
}

withContext是一个挂起函数,它会暂停当前协程的执行,等待传递进来的协程上下文切换后继续执行。当在withContext内部发生异常时,异常会被传递回到withContext函数的调用者,也就是当前协程的上一级代码中,进而可以被try-catch块捕获到。

2、launch

import kotlinx.coroutines.*

fun main() = runBlocking {
    try {
        launch {
            println("launch start")
            // 可能抛出异常
            println("launch end")
        }
    } catch (ex: Exception) {
        println("launch caught: ${ex.message}")
    }
}

try {
    GlobalScope.launch {
        throw NullPointerException()
    }
} catch (e :Exception) {
    e.printStackTrace()
}

launch启动的协程是独立于调用它的协程之外的一个新的协程,它没有直接的上级协程来捕获它的异常,因此try-catch在协程外部捕获不到协程中的异常。
事实证明,只要是launch的协程,无论是子协程还是根协程,都无法被捕获。比如:

GlobalScope.launch {
    try {
        launch {
            Log.d("MainActivity_", "launch-> threadName->" + Thread.currentThread().name)
            throw NullPointerException()
        }
    } catch (e :Exception) {
        e.printStackTrace()
    }
}

一样会崩溃。

如果将try catch放于内部:

import kotlinx.coroutines.*

fun main() = runBlocking {
     launch {
         try {
            // 可能抛出异常
         } catch (ex: Exception) {
            println("launch caught: ${ex.message}")
         }
         println("launch end")
	 }
}

这样便可以捕获得到异常了。

3、async

(1)内部async

GlobalScope.launch {
    try {
        val deferredResult: Deferred<Int> = async {
            Log.d("AsyncTest", "throw before")
            throw Exception("async function exception")
            Log.d("AsyncTest", "throw after")
        }
        deferredResult.await()
    } catch (ex: Exception) {
        Log.d("AsyncTest", "${ex.message}")
    }
}

输出:

D/AsyncTest: throw before
D/AsyncTest: async function exception

但是程序奔溃了,可以捕获异常,但是会崩。


(2)、将try catch放于内部:

GlobalScope.launch {
    val deferredResult: Deferred<Int> = async {
        try {
            Log.d("AsyncTest", "throw before")
            throw Exception("async function exception")
            Log.d("AsyncTest", "throw after")
        } catch (e: java.lang.Exception) {
            Log.d("AsyncTest", "${e.message}")
        }
    }
    deferredResult.await()
}

输出:

D/AsyncTest: throw before
D/AsyncTest: async function exception

可以捕获异常,并且程序不会崩溃。


(3)、使用GlobalScope.async

GlobalScope.launch {
    try {
        val deferredResult: Deferred<Int> = GlobalScope.async {
            Log.d("AsyncTest", "throw before")
            throw Exception("async function exception")
            Log.d("AsyncTest", "throw after")
        }
        deferredResult.await()
    } catch (ex: Exception) {
        Log.d("AsyncTest", "${ex.message}")
    }
}

输出:

D/AsyncTest: throw before
D/AsyncTest: async function exception

可以捕获异常,并且程序不会崩溃。


(4)、只对deferredResult.await()try catch

GlobalScope.launch {
    val deferredResult: Deferred<Int> = GlobalScope.async {
        Log.d("AsyncTest", "throw before")
        throw Exception("async function exception")
        Log.d("AsyncTest", "throw after")
    }
    try {
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Glide源码解析三(注册组件) 下一篇kotlin协程异常处理之-CoroutineE..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目