I see multiple sources claiming that an exception happening inside an async{} block is not delivered anywhere and only stored in the Deferred
instance. The claim is that the exception remains "hidden" and only influences things outside at the moment where one will call await()
. This is often described as one of the main differences between launch{}
and async{}
. Here is an example.
An uncaught exception inside the async code is stored inside the resulting Deferred and is not delivered anywhere else, it will get silently dropped unless processed
According to this claim, at least the way I understand it, the following code should not throw, since no-one is calling await:
// throws
runBlocking {
async { throw Exception("Oops") }
}
And yet, the exception gets thrown. This is also discussed here, but I could not really understand why by reading this.
So it seems to me that when async throws, a "cancelation signal" is propagated on the parent scope, even if await()
does not get called. Aka the exception does not really remain hidden, nor silently dropped, as the quote above states. Is my assumption correct?
Now, if we pass a SupervisorJob()
, the code does not throw:
// does not throw
runBlocking {
async(SupervisorJob()) { throw Exception("Oops") }
}
This seems reasonable since supervisor job is meant to swallow failures.
And now comes the part I do not understand at all. If we pass Job()
, the code still runs without throwing, even though Job()
is supposed to propagate failures to its parent scope:
// does not throw. Why?
runBlocking {
async(Job()) { throw Exception("Oops") }
}
So my question is, why passing no Job throws, but passing either Job or SupervisorJob does not throw?