-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix inconsistencies in the documentation on exception handling and cancellation #871
Labels
docs
KDoc and API reference
Comments
Ok, this is related to a question I asked today on coroutine slack channel. The questions is : "why In 'uncaughtExceptionHandler' ... is called ?" import kotlinx.coroutines.*
import kotlinx.coroutines.channels.produce
// From : https://kotlinlang.org/docs/reference/coroutines/exception-handling.html
//
// Coroutine builders come in two flavors: propagating exceptions automatically (launch and actor) or
// exposing them to users (async and produce).
//
// The former treat exceptions as unhandled, similar to Java's Thread.uncaughtExceptionHandler,
// while the latter are relying on the user to consume the final exception,
// for example via await or receive (produce and receive are covered later in Channels section).
fun main() {
val job = GlobalScope.launch(CoroutineExceptionHandler { _, t -> println("In 'uncaughtExceptionHandler' $t") }) {
try {
val ch = doJob2()
for (e in ch) {
println("Receive $e")
}
} catch (e: Exception) {
println("Catched exception $e, not rethrown")
}
}
runBlocking { job.join() }
}
fun CoroutineScope.doJob2() = produce<String> {
for (i in 0..10) {
println("Send $i")
send("Emit $i")
if (i == 3) close(IllegalStateException("Haha !"))
}
}
|
Now the question is : how to prevent exception to be considered unhandled ? |
elizarov
added a commit
that referenced
this issue
Apr 1, 2020
* Consistent terminology on "uncaught exceptions". * Clarified special relations of exception handling with supervision. * Clearer text in CoroutineExceptionHandler examples. * Minor stylistic corrections. Fixes #871
I've made another pass over |
qwwdfsad
pushed a commit
that referenced
this issue
Apr 24, 2020
* Further clarifications and better style for exception handling * Consistent terminology on "uncaught exceptions". * Clarified special relations of exception handling with supervision. * Clearer text in CoroutineExceptionHandler examples. * Minor stylistic corrections. Fixes #1746 Fixes #871 Co-Authored-By: EdwarDDay <4127904+EdwarDDay@users.noreply.github.com>
recheej
pushed a commit
to recheej/kotlinx.coroutines
that referenced
this issue
Dec 28, 2020
* Further clarifications and better style for exception handling * Consistent terminology on "uncaught exceptions". * Clarified special relations of exception handling with supervision. * Clearer text in CoroutineExceptionHandler examples. * Minor stylistic corrections. Fixes Kotlin#1746 Fixes Kotlin#871 Co-Authored-By: EdwarDDay <4127904+EdwarDDay@users.noreply.github.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These are a few quotes from exception-handling.html in the official docs:
(1)
(2)
(3)
These sentences convey a confusing message.
(1) seems to claim that
launch
andasync
are different andasync
won't propagate the exceptions, relying on the user to receive them in anawait()
.(2) seems to contradict that and claim that exceptions always cancel their coroutine and propagate to the parent.
(3) seems to once again make a difference between
launch
andasync
.The actual behavior is that all builders propagate the exception to their parent, so the claim in (1) is incorrect. That in turn means that all exceptions are equally treated as "unhandled", which defeats the rationale given in (3). Yet the actual behavior is as stated,
async
ignores exception handlers.The text was updated successfully, but these errors were encountered: