louiscad
09/28/2018, 1:55 PMCancellationException
is caused by a function call or by the cancellation of the coroutine scope?
I'd like to handle cancellation caused by a function call without stopping suspending iteration, unless the whole scope is cancelled (because the Activity is destroyed)elizarov
09/28/2018, 2:00 PMCancellationException
is only thrown when the scope (job) is cancelledlouiscad
09/28/2018, 2:10 PMcancel()
from a CompletableDeferred
that is awaited.
I had a while(true) try { ... } catch(e: Exception) { ... }
, but I added a coroutineScope { ... }
and did a while(isActive)
instead to fix. BTW, I realized I could have made an infinite cancellation loop by adding cancellation support to a method without allowing cancellation to ever succeed.elizarov
09/28/2018, 2:12 PMCancellationException
is thrown on cancellation. So if you are writing an infinite loop and you want it to be cancellable, then you can either check for isActive
or break when you catch CancellationException
louiscad
09/28/2018, 2:25 PMCancellationException
, then caught again, and so on...
Reading your words, I think I'm realizing it's not a good idea to throw a CancellationException
that can't be disambiguated by its type like TimeoutCancellationException
can for things like an alert dialog being dismissed so I'll create a CancellationException
subclass and cancel the CompletableDeferred
with it, so a coroutine calling it can recover safely if needed.