Overview - CancellationException behavior
What is it?
CancellationException is a special exception in Kotlin coroutines that signals a coroutine has been cancelled. It is used internally to stop coroutine execution cleanly without treating cancellation as an error. When a coroutine is cancelled, this exception is thrown to unwind the coroutine's work and release resources.
Why it matters
Without CancellationException, cancelling coroutines would be messy and error-prone, as cancellation would look like a failure or crash. This exception allows coroutines to stop work cooperatively and cleanly, improving app responsiveness and resource management. It helps developers write asynchronous code that can be stopped safely when no longer needed.
Where it fits
Learners should know basic Kotlin syntax and coroutine concepts like launching and suspending functions before this. After understanding CancellationException, they can learn advanced coroutine cancellation patterns, structured concurrency, and exception handling in coroutines.