0
0
Android Kotlinmobile~20 mins

Exception handling in coroutines in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Coroutine Exception Propagation
What happens when an exception is thrown inside a launch coroutine builder without a try-catch block?
AThe exception is propagated to the parent coroutine and can cancel the entire scope.
BThe exception is silently ignored and the coroutine completes normally.
CThe exception is caught automatically and logged without affecting other coroutines.
DThe exception causes the app to crash immediately without any propagation.
Attempts:
2 left
💡 Hint
Think about how structured concurrency manages errors in coroutines.
ui_behavior
intermediate
2:00remaining
Effect of Exception in async Coroutine
Consider this code snippet:
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?
ANo output, the exception is ignored.
BCaught: null
CThe app crashes with IllegalStateException.
DCaught: Error
Attempts:
2 left
💡 Hint
Remember that async exceptions are deferred until await() is called.
lifecycle
advanced
2:00remaining
Handling Exceptions with CoroutineExceptionHandler
Given this code:
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?
ACaught by handler: Failure
BNo output, exception is ignored.
CThe app crashes with RuntimeException.
DCaught by handler: null
Attempts:
2 left
💡 Hint
Check how CoroutineExceptionHandler works with launch coroutines.
🔧 Debug
advanced
2:00remaining
Why Does This Coroutine Exception Crash the App?
Analyze this code:
runBlocking {
  val job = launch {
    throw ArithmeticException("Divide by zero")
  }
  job.join()
  println("Coroutine finished")
}

What happens when this runs?
AThe exception is caught internally and "Coroutine finished" is printed.
BThe app crashes with ArithmeticException and "Coroutine finished" is not printed.
C"Coroutine finished" is printed and the exception is ignored.
DThe exception is logged but the app continues normally.
Attempts:
2 left
💡 Hint
Consider how exceptions in launch coroutines affect the parent scope.
🧠 Conceptual
expert
2:00remaining
Exception Handling Differences Between launch and async
Which statement correctly describes the difference in exception handling between launch and async coroutines?
ABoth <code>launch</code> and <code>async</code> defer exceptions until their job is joined.
B<code>async</code> immediately throws exceptions to the parent, while <code>launch</code> defers exceptions until <code>join()</code> is called.
C<code>launch</code> immediately throws exceptions to the parent, while <code>async</code> defers exceptions until <code>await()</code> is called.
DNeither <code>launch</code> nor <code>async</code> propagate exceptions to the parent coroutine.
Attempts:
2 left
💡 Hint
Think about when exceptions become visible in each coroutine builder.