Cancellation can suppress other exceptions, which ...
# coroutines
y
Cancellation can suppress other exceptions, which results in them staying unreported. Is this really intentional behaviour?
Copy code
launch {
  Foo().use {
    cancel()
    ensureActive()
  }
}.join()
class Foo : AutoCloseable {
  override fun close() {
    error("Foo")
  }
}
IMO,
use
should prioritize the
close
exception over a
CancellationException
. This is because cancellation is considered "normal", while an
error
is truly exceptional, and thus should be reported. The docs say: > Thrown by cancellable suspending functions if the coroutine is cancelled while it is suspended. It indicates normal cancellation of a coroutine. "_normal_" is the keyword there. Normal cancellation shouldn't mask a real exception. To be clear, this is all from the suppression behaviour of
use
. My proposal is that
use
should never
addSuppressed
to a
CancellationException
. If you use
try-finally
instead of
use
, the code reports an error as you'd expect. Error suppression is great, and it prioritising the body of
use
is great too, but it shouldn't prioritise
CancellationException
since that's an expected exception (dare I say, it's a control flow exception, which Scala also deprioritises similarly)
👍 2