0
0
Kotlinprogramming~5 mins

CancellationException behavior in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is CancellationException in Kotlin coroutines?

CancellationException is a special exception used to indicate that a coroutine was cancelled. It is not treated as a failure but as a normal way to stop coroutine execution.

Click to reveal answer
intermediate
How does CancellationException affect coroutine job completion?

When a coroutine throws CancellationException, its job is considered cancelled but not failed. This means the coroutine ends normally from the perspective of structured concurrency.

Click to reveal answer
intermediate
Why should CancellationException usually not be caught and ignored inside coroutines?

Because catching and ignoring CancellationException prevents the coroutine from properly cancelling, which can cause resource leaks or unexpected behavior.

Click to reveal answer
beginner
What happens if a coroutine throws an exception other than CancellationException?

Exceptions other than CancellationException are treated as failures, causing the coroutine's job to fail and propagate the error to its parent or handler.

Click to reveal answer
intermediate
How can you properly handle cleanup when a coroutine is cancelled?

Use try-finally blocks or invokeOnCompletion handlers to run cleanup code. The finally block will run even if CancellationException is thrown.

Click to reveal answer
What does CancellationException indicate in Kotlin coroutines?
AThe coroutine was paused
BThe coroutine failed with an error
CThe coroutine was cancelled normally
DThe coroutine completed successfully
If a coroutine throws CancellationException, what is the job's state?
ACancelled
BFailed
CCompleted successfully
DRunning
Why should you avoid catching CancellationException without rethrowing it?
AIt converts cancellation to success
BIt causes the coroutine to run twice
CIt improves performance
DIt prevents proper coroutine cancellation
Which block is guaranteed to run even if a coroutine is cancelled?
Atry
Bfinally
Ccatch
Delse
What happens if a coroutine throws an exception other than CancellationException?
AThe coroutine fails and propagates the error
BThe coroutine is cancelled normally
CThe exception is ignored
DThe coroutine restarts
Explain the role of CancellationException in Kotlin coroutines and how it differs from other exceptions.
Think about how cancellation is a normal stop, not an error.
You got /4 concepts.
    Describe best practices for handling CancellationException and cleanup in coroutines.
    Focus on how to keep cancellation working smoothly and clean up resources.
    You got /4 concepts.