Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
How does exception propagation works on CoroutineScope.async?

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?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda