Challenge - 5 Problems
Coroutine Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding Coroutine Exception Propagation
What happens when an exception is thrown inside a
launch coroutine builder without a try-catch block?Attempts:
2 left
💡 Hint
Think about how structured concurrency manages errors in coroutines.
✗ Incorrect
In Kotlin coroutines, exceptions thrown in a
launch coroutine propagate to the parent scope and can cancel sibling coroutines. This is part of structured concurrency to avoid silent failures.❓ ui_behavior
intermediate2:00remaining
Effect of Exception in
async CoroutineConsider this code snippet:
What will be printed when this code runs?
val deferred = async {
throw IllegalStateException("Error")
}
runBlocking {
try {
deferred.await()
} catch (e: Exception) {
println("Caught: ${e.message}")
}
}What will be printed when this code runs?
Attempts:
2 left
💡 Hint
Remember that
async exceptions are deferred until await() is called.✗ Incorrect
Exceptions inside
async coroutines are captured and only thrown when await() is called. The try-catch around await() catches the exception and prints the message.❓ lifecycle
advanced2:00remaining
Handling Exceptions with CoroutineExceptionHandler
Given this code:
What will be printed?
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught by handler: ${exception.message}")
}
val job = GlobalScope.launch(handler) {
throw RuntimeException("Failure")
}
runBlocking { job.join() }What will be printed?
Attempts:
2 left
💡 Hint
Check how CoroutineExceptionHandler works with
launch coroutines.✗ Incorrect
CoroutineExceptionHandler catches uncaught exceptions in
launch coroutines. The handler prints the exception message when the coroutine throws.🔧 Debug
advanced2:00remaining
Why Does This Coroutine Exception Crash the App?
Analyze this code:
What happens when this runs?
runBlocking {
val job = launch {
throw ArithmeticException("Divide by zero")
}
job.join()
println("Coroutine finished")
}What happens when this runs?
Attempts:
2 left
💡 Hint
Consider how exceptions in
launch coroutines affect the parent scope.✗ Incorrect
Exceptions in
launch coroutines are propagated and cancel the parent scope unless caught. Here, no catch block exists, so the app crashes and the print statement is skipped.🧠 Conceptual
expert2:00remaining
Exception Handling Differences Between
launch and asyncWhich statement correctly describes the difference in exception handling between
launch and async coroutines?Attempts:
2 left
💡 Hint
Think about when exceptions become visible in each coroutine builder.
✗ Incorrect
launch coroutines propagate exceptions immediately to their parent, potentially cancelling sibling coroutines. async coroutines capture exceptions and only throw them when await() is called.