Exception handling: launch vs async

In the docs, it says;

Coroutine builders come in two flavors: propagating exceptions automatically (launch and actor) or exposing them to users (async and produce). The former treat exceptions as unhandled, similar to Java’s Thread.uncaughtExceptionHandler , while the latter are relying on the user to consume the final exception, for example via await or receive (produce and receive are covered later in Channels section).

I don’t quite get the difference between launch and async in terms of exception handling. In either case, if the exception is not handled, program crashes. It only works the way it’s documented if you run these coroutines in the GlobalScope as, probably, they run on their own in a fire and forget fashion and without a parent.

If that’s the case, I think the docs might be a bit misleading (of course if I’m not mistaken).

These coroutines fail on different scope, in particular GlobalScope cannot fail or cancelled.

Regarding the documentation, launch and actor does not return nothing, never, so an exception handler is required to understand why it was failed.
Instead async and produce expect an use of its result, or a handling of its exception.

For the structured concurrency the only difference is in the Job, or in the SupervisorJob.

1 Like

I kind of get the idea. You can catch the exception using whenever you call await with async which is different with launch where you need to catch right on the execution of it.