Why exception handling with Kotlin Coroutines is so hard, how to master it!
summary
Kotlin coroutines propagate uncaught exceptions up the Job hierarchy so installing a CoroutineExceptionHandler at root only catches failures; catching inside a child coroutine prevents propagation and can hide errors or stop sibling cancellation (e.g., two async network calls where catching the first lets the second continue and wastes work). async exceptions are captured in a Deferred and rethrow on await — async exceptions live in Deferred — and supervisorScope isolates failures to its supervisor job, so always rethrow CancellationException when you intend cancellation to stop execution.
AI-written summary · about
chapters (10)
- 0:00Introduction
- 2:11Why exception handling is hard
- 7:59Structured concurrency and job hierarchy
- 10:08Coroutine exception handler
- 14:47Try-catch vs exception handler
- 16:56Cancellation and recovery
- 20:48Async vs launch behavior
- 26:58Scoping functions
- 32:58Recap and key points
- 36:52Resources and closing
related talks
37mCoroutines! Gotta catch 'em all!
39mMistakes you make using Kotlin Coroutines
42mKotlin Coroutines: Beyond async/await
39mKotlin Coroutine Mechanisms
32mStructured Concurrency: The paradigm shift
59mKotlin coroutines: new ways to do asynchronous programming
30mBetter asynchronous programming with Kotlin Coroutines
44mTesting Kotlin Coroutines
