tezvyn:

Coroutine Exception Handling: launch vs. async

Source: kotlinlang.orgadvanced

Coroutine builders handle exceptions differently. `launch` propagates them immediately, crashing if unhandled. `async` silently catches them, waiting for you to call `await`. Use a `CoroutineExceptionHandler` on root scopes for global logging.

Coroutine builders have two exception philosophies. `launch` is 'fail-fast', propagating exceptions up the job hierarchy immediately, crashing the app if unhandled at the root. `async` is 'handle-later', catching exceptions and storing them in its `Deferred` result until you call `await`. This lets you handle errors gracefully where the result is consumed. The main footgun is that a `CoroutineExceptionHandler` only works on root coroutines using `launch`; it's ignored on children and has no effect on `async`.

Read the original → kotlinlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Coroutine Exception Handling: launch vs. async · Tezvyn