0
0
Kotlinprogramming~20 mins

CoroutineExceptionHandler in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CoroutineExceptionHandler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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")
}
ANo output
BException is not caught, program crashes
C
Caught: My custom error
Done
D
Done
Caught: My custom error
Attempts:
2 left
💡 Hint
CoroutineExceptionHandler catches uncaught exceptions in coroutines launched with it.
Predict Output
intermediate
2: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}")
    }
}
A
Caught: Error in async
Caught in try: Error in async
BCaught in try: Error in async
CCaught: Error in async
DNo output, program hangs
Attempts:
2 left
💡 Hint
CoroutineExceptionHandler does not catch exceptions from async builder.
🔧 Debug
advanced
2: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") }
ABecause the handler was not passed to launch, so it can't catch exceptions
BBecause runBlocking swallows exceptions silently
CBecause RuntimeException is not caught by CoroutineExceptionHandler
DBecause job.join() suppresses exceptions
Attempts:
2 left
💡 Hint
CoroutineExceptionHandler must be passed to the coroutine builder to work.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a CoroutineExceptionHandler?
Select the correct Kotlin syntax to create a CoroutineExceptionHandler that prints the exception message.
Aval handler = CoroutineExceptionHandler { exception -> println("Error: ${exception.message}") }
Bval handler = CoroutineExceptionHandler { (context, exception) -> println("Error: ${exception.message}") }
Cval handler = CoroutineExceptionHandler(context, exception) -> println("Error: ${exception.message}")
Dval handler = CoroutineExceptionHandler { context, exception -> println("Error: ${exception.message}") }
Attempts:
2 left
💡 Hint
CoroutineExceptionHandler takes a lambda with two parameters: CoroutineContext and Throwable.
🚀 Application
expert
3: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)
}
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
CoroutineExceptionHandler catches uncaught exceptions in launch coroutines but not in async or caught exceptions.