Hi, I'm trying to confirm something: When I call J...
# coroutines
o
Hi, I'm trying to confirm something: When I call Job.join() are regular exceptions rethrown? I would expect them to be, but the documentation says:
This invocation resumes normally (without exception) when the job is complete for any reason and the Job of the invoking coroutine is still active.
m
No the exception is sent to the exception handler of the coroutine. https://kotlinlang.org/docs/exception-handling.html#exception-propagation On Android it will cause a crash on the JVM it will print to console. I believe it crashes on iOS also. Now on Android, I think the viewModelScope is configured to handle the exception and not crash
o
That's wrong.
Job.join()
will see the exception.
Playground example.
d
Job.join()
does not rethrow the exception with which the
Job
finished. That said, because it's a
suspend
function, it will throw a
CancellationException
if the caller is cancelled. Oliver's example throws an exception because
job1
is the child of
coroutineScope
, its failure cancels the
coroutineScope
, and therefore, the computation in the
coroutineScope
gets cancelled as well. If a
Job
fails independently from the
join
caller (e.g., they have unrelated coroutine scopes), then no exception will be thrown! https://pl.kotl.in/Bis4BithQ is an example.
thank you color 2