Why Exception Handling with Kotlin Coroutines is hard and how to master it
summary
Explains that exceptions in Kotlin coroutines travel the job hierarchy instead of being rethrown, so a try/catch in a parent often misses a failing child; demonstrates fixes using CoroutineExceptionHandler installed on root coroutines, and shows how async/Deferred encapsulates exceptions until await while launch sends them to the root handler. Shows concrete pitfalls: two concurrent async network requests where catching errors prevents sibling cancellation, and a 500ms cancel vs 1000ms delay example that requires rethrowing CancellationException.
AI-written summary · about
chapters (22)
- 0:00Introduction
- 0:57What are Kotlin coroutines
- 2:22Basics of exception handling
- 4:51Exception handling in coroutines
- 6:17Why try-catch fails in coroutines
- 7:41Job hierarchy explained
- 9:30Coroutine exception handler
- 10:13Installing exception handler
- 13:29Root coroutines specialties
- 14:11Try-catch vs exception handler
- 16:17Cancellation mechanism impact
- 18:26Problem with try-catch in async
- 20:15Catching cancellation exceptions
- 22:48Launch vs async differences
- 23:31Async root coroutine behavior
- 25:38Async as child coroutine
- 26:41Coroutine scope scoping function
- 27:46Supervisor scope explained
- 29:36Supervisor scope exception handling
- 30:57Key takeaways recap
- 33:04Resources and cheat sheet
- 34:30Q&A session
