Summary
In this chapter, you learned about coroutine cancelations. You can cancel coroutines by using the cancel
or cancelAndJoin
function from the coroutine job or the cancel
function from the coroutine scope.
You learned that a coroutine cancelation needs to be cooperative. You also learned how you can change your code to make your coroutine cancelable by using isActive
checks or by using suspending functions from the kotlinx.coroutines
package.
Then, you learned about coroutine timeouts. You can set a timeout (in milliseconds or Duration
) using withTimeout
or withTimeoutOrNull
.
You also learned about coroutine exceptions and how to catch them. try-catch
blocks can be used to handle exceptions. You can also use CoroutineExceptionHandler
in your coroutine scope to catch and handle exceptions in a single location.
Finally, you worked on an exercise to add cancelation to a coroutine and another exercise to update your code to handle coroutine exceptions.
In the next...