If I have a `SupervisorJob`, how can I stop all it...
# coroutines
c
If I have a
SupervisorJob
, how can I stop all its children without an exception? If I use
supervisorJob.cancel()
, an exception is thrown.
a
How else would you do it if not by resuming any outstanding associated continuations with an exception? All of the suspend functions being called expect to have their finally blocks run eventually to clean up, you don't know generically what kind of return value to resume them with as an alternative, etc.
l
There's the cancelChildren function on the underlying Job, but it's rarely the right way to satisfy the actual use cases.
c
@Adam Powell That does make a lot of sense, thanks. Is it good practice, then, to just catch that exception and ignore it?
a
Why would you catch it?
l
You say there's an exception thrown by
cancel()
? What is its message?
z
The initial question is ambiguous about where the exception is thrown from. If it’s thrown from
cancel()
itself, that seems odd. If the exception is thrown from somewhere in the cancelled coroutines, and it’s a
CancellationException
, that’s what Adam is talking about, and is how the coroutine cancellation mechanism is designed. The proper thing to do from coroutines is to pass `CancellationException`s through (either don’t catch them at all, or if you do catch them, rethrow them immediately).
👍 1
c
Thanks. That is what is currently happening, so everything is good, I just misread the exception's source.