- 协程异常处理以确保部分协程出错时其他协程正常运行:
- 使用
try - catch
块:在每个协程内部,可以使用try - catch
块来捕获异常。这样当一个协程内部发生异常时,该协程的异常不会传播到其他协程。例如:
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
try {
// 模拟可能抛出异常的任务
throw RuntimeException("Task 1 failed")
} catch (e: Exception) {
println("Caught exception in task 1: $e")
}
}
launch {
delay(100)
println("Task 2 is running fine")
}
}
- 使用
CoroutineExceptionHandler
:可以为CoroutineScope
设置CoroutineExceptionHandler
来全局捕获协程中的未处理异常。例如:
import kotlinx.coroutines.*
val exceptionHandler = CoroutineExceptionHandler { _, exception ->
println("Caught exception: $exception")
}
fun main() = runBlocking {
GlobalScope.launch(exceptionHandler) {
throw RuntimeException("Task failed")
}
launch {
delay(100)
println("Another task is running fine")
}
}
- 使用
async
和await
实现并发任务并获取结果:
async
用于启动一个异步任务并返回一个Deferred
对象,await
用于等待Deferred
对象完成并获取其结果。例如:
import kotlinx.coroutines.*
fun main() = runBlocking {
val deferred1 = async {
delay(1000)
"Result of task 1"
}
val deferred2 = async {
delay(2000)
"Result of task 2"
}
val result1 = deferred1.await()
val result2 = deferred2.await()
println("$result1 and $result2")
}
- 处理
async
任务执行过程中的异常:
- 使用
try - catch
块:可以在调用await
的地方使用try - catch
块来捕获async
任务执行过程中抛出的异常。例如:
import kotlinx.coroutines.*
fun main() = runBlocking {
val deferred = async {
throw RuntimeException("Task failed")
}
try {
val result = deferred.await()
println(result)
} catch (e: Exception) {
println("Caught exception: $e")
}
}
- 在
async
内部使用try - catch
:也可以在async
块内部使用try - catch
,然后返回一个合适的结果(比如null
或者错误信息)。例如:
import kotlinx.coroutines.*
fun main() = runBlocking {
val deferred = async {
try {
throw RuntimeException("Task failed")
"Normal result"
} catch (e: Exception) {
"Error: $e"
}
}
val result = deferred.await()
println(result)
}