Challenge - 5 Problems
CancellationException Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output when a coroutine is cancelled with CancellationException?
Consider the following Kotlin coroutine code. What will be printed to the console?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val job = launch { try { repeat(5) { i -> println("Working on $i...") delay(100) } } catch (e: CancellationException) { println("Cancelled with message: ${e.message}") throw e } finally { println("Finally block executed") } } delay(250) job.cancel(CancellationException("Stop now")) job.join() println("Main ends") }
Attempts:
2 left
💡 Hint
Think about what happens when a coroutine is cancelled and how CancellationException is handled.
✗ Incorrect
The coroutine prints "Working on 0..." and "Working on 1..." before cancellation. When cancelled with a CancellationException carrying the message "Stop now", the catch block prints the message, rethrows the exception, and finally block runs. Then main prints "Main ends".
❓ Predict Output
intermediate2:00remaining
What happens if CancellationException is caught but not rethrown?
Look at this Kotlin coroutine snippet. What will be the output?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val job = launch { try { repeat(3) { i -> println("Step $i") delay(100) } } catch (e: CancellationException) { println("Caught cancellation: ${e.message}") // Not rethrowing } finally { println("Cleanup in finally") } } delay(150) job.cancel(CancellationException("Cancelled early")) job.join() println("Done") }
Attempts:
2 left
💡 Hint
Consider what happens if the CancellationException is caught but not rethrown in a coroutine.
✗ Incorrect
If CancellationException is caught but not rethrown, the coroutine does not cancel properly and finishes normally after the catch block. The finally block runs, then main prints "Done".
❓ Predict Output
advanced2:00remaining
What error is thrown if a coroutine is cancelled with a non-CancellationException?
Analyze this Kotlin coroutine code. What exception will be thrown?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val job = launch { try { delay(1000) } catch (e: CancellationException) { println("Cancelled") throw e } } delay(100) job.cancel(RuntimeException("Not a cancellation")) job.join() }
Attempts:
2 left
💡 Hint
What happens if you cancel a coroutine with an exception that is not a CancellationException?
✗ Incorrect
Cancelling a coroutine with a non-CancellationException causes that exception to be thrown and propagated. The catch block for CancellationException is not triggered because the exception is not of that type.
🧠 Conceptual
advanced2:00remaining
Why should CancellationException be rethrown in coroutine catch blocks?
In Kotlin coroutines, why is it important to rethrow CancellationException after catching it?
Attempts:
2 left
💡 Hint
Think about how coroutine cancellation propagates through coroutine hierarchies.
✗ Incorrect
Rethrowing CancellationException ensures the coroutine is marked as cancelled and the cancellation signal propagates to parent coroutines. Not rethrowing can cause the coroutine to continue running unexpectedly.
❓ Predict Output
expert2:00remaining
What is the output when a coroutine's finally block throws a CancellationException?
Examine this Kotlin coroutine code. What will be printed or thrown?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val job = launch { try { delay(100) } finally { println("In finally") throw CancellationException("From finally") } } try { job.join() } catch (e: CancellationException) { println("Caught in main: ${e.message}") } }
Attempts:
2 left
💡 Hint
Consider how exceptions thrown in finally blocks are handled in coroutines.
✗ Incorrect
The finally block runs and prints "In finally", then throws a CancellationException. This exception is caught in main's try-catch, printing the message.