Exception Handling
Explore how to manage exceptions and cancellations in Kotlin coroutines. Understand how exceptions propagate between parent and child coroutines and learn to use SupervisorJob to prevent the entire coroutine hierarchy from canceling, enhancing your control over coroutine behavior.
We'll cover the following...
A significant part of how coroutines behave is their exception handling. Just as a program breaks when an uncaught exception slips by, a coroutine breaks in the case of an uncaught exception. This behavior is nothing new—threads also end in such cases. The difference is that coroutine builders also cancel their parents, and each canceled parent cancels all its children. Let’s look at the example below.
Once a coroutine receives an exception, it cancels itself and propagates the ...