当前位置:网站首页>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.
边栏推荐
猜你喜欢
Learning Notes---Machine Learning
重要消息丨.NET Core 3.1 将于今年12月13日结束支持
学习小笔记---机器学习
CoCube传感器MPU6050笔记
搭载开源鸿蒙系统的嵌入式XM-RK3568工业互联方案
虚拟机网卡报错:Bringing up interface eth0: Error: No suitable device found: no device found for connection
MYSQLWorkbench看数据库ER图
Laravel文档阅读笔记-Rendering JSON(对JS变量进行赋值)
74HC595芯片引脚说明
SSM整合开发案例
随机推荐
重要消息丨.NET Core 3.1 将于今年12月13日结束支持
unity第一课
MySQL高级特性之分布式(XA)事务的介绍
排序第四节——归并排序(附有自己的视频讲解)
DSP+ARM+FPGA高速PCIE/千兆网口信号仿真介绍
4.MySQL更新和删除数据
常用测试用例设计方法之正交实验法详解
Lottie系列一:介绍与使用
MongDb query method
2019南昌网络赛 C题,Hello 2019
way of thinking problem-solving skills
Flexible and easy-to-use sql monitoring script part7
TCP段重组PDU
Win10桌面图标排列混乱
RestFul,会话技术,Fiddler
01 自然语言处理NLP介绍
【Template】Tree Chain Segmentation P3384
学习小笔记---机器学习
Classes and Structures
【转载】Deep Learning(深度学习)学习笔记整理