当前位置:网站首页>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.:

  1. When an exception occurs, first cancel all of its child coroutines, then cancel itself, and then pass the exception to the parent coroutine;
  2. The parent coroutine first cancels all its own child coroutines, then cancels itself, and then passes it up to the root coroutine.
launchasync
as root coroutineException thrown directlyExceptions are thrown only by calling await() after final consumption
As a child coroutineThe 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.

原网站

版权声明
本文为[lazy programmer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090725147787.html