Challenge - 5 Problems
CoroutineExceptionHandler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this CoroutineExceptionHandler example?
Consider the following Kotlin code using CoroutineExceptionHandler. What will be printed when this code runs?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val handler = CoroutineExceptionHandler { _, exception -> println("Caught: ${exception.message}") } val job = GlobalScope.launch(handler) { throw AssertionError("My custom error") } job.join() println("Done") }
Attempts:
2 left
💡 Hint
CoroutineExceptionHandler catches uncaught exceptions in coroutines launched with it.
✗ Incorrect
The CoroutineExceptionHandler catches the AssertionError thrown inside the coroutine and prints the message. Then the main coroutine prints "Done" after job.join().
❓ Predict Output
intermediate2:00remaining
What happens if CoroutineExceptionHandler is used with async builder?
Look at this Kotlin code snippet. What will be the output or behavior?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val handler = CoroutineExceptionHandler { _, exception -> println("Caught: ${exception.message}") } val deferred = async(handler) { throw IllegalStateException("Error in async") } try { deferred.await() } catch (e: Exception) { println("Caught in try: ${e.message}") } }
Attempts:
2 left
💡 Hint
CoroutineExceptionHandler does not catch exceptions from async builder.
✗ Incorrect
Exceptions in async coroutines are deferred until await is called. CoroutineExceptionHandler does not catch these exceptions. The exception is caught in the try-catch around await.
🔧 Debug
advanced2:00remaining
Why does this CoroutineExceptionHandler not catch the exception?
This Kotlin code uses CoroutineExceptionHandler but the exception is not caught by it. Why?
fun main() = runBlocking {
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught: ${exception.message}")
}
val job = launch {
throw RuntimeException("Oops")
}
job.join()
println("Finished")
}
Attempts:
2 left
💡 Hint
CoroutineExceptionHandler must be passed to the coroutine builder to work.
✗ Incorrect
The handler is defined but not used in launch. Without passing it as a context element, exceptions are not caught by it.
📝 Syntax
advanced2:00remaining
Which option correctly creates a CoroutineExceptionHandler?
Select the correct Kotlin syntax to create a CoroutineExceptionHandler that prints the exception message.
Attempts:
2 left
💡 Hint
CoroutineExceptionHandler takes a lambda with two parameters: CoroutineContext and Throwable.
✗ Incorrect
Option D uses the correct lambda syntax with two parameters. Options A, C, and D have syntax errors or wrong parameter usage.
🚀 Application
expert3:00remaining
How many exceptions will be caught by this CoroutineExceptionHandler?
Given this Kotlin code, how many exceptions will the CoroutineExceptionHandler catch and print?
Kotlin
import kotlinx.coroutines.* fun main() = runBlocking { val handler = CoroutineExceptionHandler { _, exception -> println("Caught: ${exception.message}") } val supervisor = SupervisorJob() val scope = CoroutineScope(coroutineContext + supervisor + handler) scope.launch { throw IllegalArgumentException("First error") } scope.launch { try { throw IllegalStateException("Second error") } catch (e: Exception) { println("Caught locally: ${e.message}") } } scope.async { throw ArithmeticException("Third error") } delay(100) }
Attempts:
2 left
💡 Hint
CoroutineExceptionHandler catches uncaught exceptions in launch coroutines but not in async or caught exceptions.
✗ Incorrect
The first launch throws an uncaught exception caught by handler. The second launch catches its own exception locally. The async throws an exception that is not caught by handler because async exceptions are deferred until await, which is not called here.