Skip to content

Commit fc7a1e5

Browse files
committed
~ Clarification on what is exception handling (no recovery)
1 parent 90a13a1 commit fc7a1e5

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

docs/exception-handling.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ Caught ArithmeticException
8080

8181
What if one does not want to print all exceptions to the console?
8282
[CoroutineExceptionHandler] context element on a _root_ coroutine can be used as generic `catch` block for
83-
this root coroutine and all its children where custom logging or exception handling may take place.
83+
this root coroutine and all its children where custom exception handling may take place.
8484
It is similar to [`Thread.uncaughtExceptionHandler`](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)).
85+
You cannot recover from the exception in the `CoroutineExceptionHandler`. The coroutine had already completed
86+
with the corresponding exception when the handler is called. Normally, the handler is used to
87+
log the exception, show some kind of error message, terminate, and/or restart the application.
8588

8689
On JVM it is possible to redefine global exception handler for all coroutines by registering [CoroutineExceptionHandler] via
8790
[`ServiceLoader`](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html).

kotlinx-coroutines-core/common/src/CoroutineExceptionHandler.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ public inline fun CoroutineExceptionHandler(crossinline handler: (CoroutineConte
6464
* ### Handling coroutine exceptions
6565
*
6666
* `CoroutineExceptionHandler` is a last-resort mechanism for global "catch all" behavior.
67+
* You cannot recover from the exception in the `CoroutineExceptionHandler`. The coroutine had already completed
68+
* with the corresponding exception when the handler is called. Normally, the handler is used to
69+
* log the exception, show some kind of error message, terminate, and/or restart the application.
70+
*
6771
* If you need to handle exception in a specific part of the code, it is recommended to use `try`/`catch` around
68-
* the corresponding code inside your coroutine, like this:
72+
* the corresponding code inside your coroutine. This way you can you prevent completion of the coroutine
73+
* with the exception (exception is now _caught_), retry the operation, and/or take other arbitrary actions:
6974
*
7075
* ```
7176
* scope.launch { // launch child coroutine in a scope

0 commit comments

Comments
 (0)