当前位置:网站首页>Kotlin Coroutines - Exception Handling
Kotlin Coroutines - Exception Handling
2022-08-09 07:32:00 【lazy programmer】
Propagation of exceptions
Job's exception propagation is bidirectional. If the exception is not caught locally, it will not be re-thrown but will continue to propagate upward. Due to the characteristics of structured concurrency, all coroutines in the hierarchy will be cancelled.:
- When an exception occurs, first cancel all of its child coroutines, then cancel itself, and then pass the exception to the parent coroutine;
- The parent coroutine first cancels all its own child coroutines, then cancels itself, and then passes it up to the root coroutine.
| launch | async | |
| as root coroutine | Exception thrown directly | Exceptions are thrown only by calling await() after final consumption |
| As a child coroutine | The exception is thrown directly | |
Interrupt transmission
Use when the exception is not expected to propagate upwards or affect sibling coroutines (downward propagation still exists).
| SupervisorScope() | supervisorJob |
| An exception in a child coroutine will cancel all coroutines in the scope, but will not affect the outside. | The exception of the child coroutine will not affect the parent and sibling coroutines. |
supervisorScope{launch{println("Subcoroutine 1")throw Exception()}launch{println("Subcoroutine 2")}}suspend fun main() = runBlocking{launch(supervisorJob()){println("Subcoroutine 1")throw Exception()}launch{println("Subcoroutine 2")}}Exception catch
try-catch partial capture | CoroutineExceptionHandler Global catch |
| can only capture specific code.Catching coroutine builders is ineffective, because of structured concurrency, there is no way to prevent cascading cancellations caused by exceptions. | Catch those unhandled exceptions in scope at the root coroutine, since the top is reached, at which point the internal chain cancellation is complete. |
CoroutineExceptionHandler
CoroutineExceptionHandler usage conditions:
- Or exists in the context of the coroutine scope
- Or exists in a direct child of the root coroutine
The exception will always be propagated up to the root coroutine. If the root coroutine does not respond (SupervisorScope() or supervisorJob), the direct sub-coroutine will look for CoroutineExceptionHandler in the coroutineContext for processing, otherwise go to UncaughtExceptionHandler, so other sub-coroutinesThe CoroutineExceptionHandler won't work.
The exception will be caught when the following conditions are met:
timing: exceptions thrown by coroutines that automatically throw exceptions (use launch instead of async);Location: In the context of a coroutine scope or in a direct child of the root coroutine.
Android global exception capture
All exceptions unhandled by coroutines can be obtained, but exceptions cannot be caught or prevented from crashing.Create the app/src/main/resources/META-INF/services directory, and create a file named kotlinx.coroutines.CoroutineExceptionHandler. The content of the file is filled with the class name of the created global exception handler.

边栏推荐
猜你喜欢
随机推荐
力扣 636. 函数的独占时间
Invoker 2019CCPC秦皇岛站I题 简单DP
【机器学习】支持向量机(SVM)代码练习
MySQL高级特性之分布式(XA)事务的介绍
makefile记录
js数组相关知识复习
dp学习笔记
leetcode:55. 跳跃游戏
Flexible and easy-to-use sql monitoring script part7
74HC595芯片引脚说明
定时任务组件Quartz
Use tensorflow.keras to build a neural network model modularly
Variable used in lambda expression should be final or effectively final报错解决方案
way of thinking problem-solving skills
c语言位段
【Reprint】Deep Learning (deep learning) study notes arrangement
找不到和chrome浏览器版本不同的chromedriver的解决方法
信息反馈平台的设计与实现(一、项目设计)
C语言:打印菱形
【修电脑】系统重装但IP不变后VScode Remote SSH连接失败解决









