https://kotlinlang.org logo
#coroutines
Title
# coroutines
c

coder82

08/21/2019, 12:26 PM
What's the correct way to call cancel on a job giving another cause so that the coroutine will fail? I tried everything but it looks like that in case of CancellationException with custom cause the coroutine doesn't trigger CoroutineFailureException, so to me it doesn't really fail..., it just cancels... the documentation says it should fail the coroutine. In my case such coroutine is a SupervisorJob child (but that shouldn't affect this behaviour).
k

kingsley

08/21/2019, 3:00 PM
cancel
always cancels the job with a cancellation exception. The coroutine will only fail if it were to throw an exception within itself other than a cancellation exception
the documentation says it should fail the coroutine
Hmm… I found this
If the cause of cancellation is a different exception, then the job is considered to have failed
but it also says
This usually happens when the code of the job encounters some problem and throws an exception.
Looking at the code for cancel(Throwable) which is now deprecated, I found this:
This method has bad semantics when cause is not a [CancellationException]
So, either you use a
cancel(CancellationException)
or a
cancel(message: String, cause: Throwable)
which gets wrapped in a CancellationException. There’s really no way to fail the coroutine from outside AFAIK
👍 1
3 Views