当前位置:网站首页>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.
边栏推荐
猜你喜欢
Unity first lesson
Inception V3 Eye Closure Detection
生成对抗网络GAN:Generative Adversarial Networks
错误:为 repo ‘oracle_linux_repo‘ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.
【机器学习】降维代码练习
【Template】Tree Chain Segmentation P3384
RestFul,会话技术,Fiddler
灵活好用的sql monitoring 脚本 part7
常见的分布式事务解决方案
【报错】Root Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
随机推荐
3.MySQL插入数据, 读取数据、Where子句和Order By关键字
Codeforces Round #359 (Div. 2) C. Robbers' watch Violent Enumeration
JSONObject遍历的时候顺序不一致,导致数据对应出错
Learning Notes---Machine Learning
DSP+ARM+FPGA高速PCIE/千兆网口信号仿真介绍
SAP ALV 数据导出被截断的bug
Neural Network Optimizer
C语言:打印菱形
tianqf's problem-solving ideas
composer 内存不足够
MVN 中配置flyway mysq
eyb:Redis学习(2)
Use tensorflow.keras to build a neural network model modularly
pip安装更换镜像
基于布朗运动的文本生成方法-LANGUAGE MODELING VIA STOCHASTIC PROCESSES
用tensorflow.keras模块化搭建神经网络模型
力扣 636. 函数的独占时间
74HC595芯片引脚说明
(本章节完结)排序第五节——非比较排序(计数排序+基数排序+桶排序)(附有自己的视频讲解)
【模板】树链剖分 P3384