0
0
Kotlinprogramming~20 mins

Exception handling in coroutines in 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!
Predict Output
intermediate
2:00remaining
Output of coroutine with try-catch
What is the output of this Kotlin coroutine code snippet?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = launch {
        try {
            delay(100)
            throw IllegalArgumentException("Error in coroutine")
        } catch (e: IllegalArgumentException) {
            println("Caught: ${e.message}")
        }
    }
    job.join()
    println("Coroutine finished")
}
AException in thread "main" java.lang.IllegalArgumentException: Error in coroutine
B
Caught: Error in coroutine
Coroutine finished
CCaught: Error in coroutine
D
Coroutine finished
Caught: Error in coroutine
Attempts:
2 left
💡 Hint
Look at where the exception is caught inside the coroutine.
Predict Output
intermediate
2:00remaining
Exception propagation in coroutine scope
What will be printed when this Kotlin code runs?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val handler = CoroutineExceptionHandler { _, exception ->
        println("Handled: ${exception.message}")
    }
    val job = launch(handler) {
        throw ArithmeticException("Divide by zero")
    }
    job.join()
    println("End of main")
}
A
Handled: Divide by zero
End of main
BException in thread "main" java.lang.ArithmeticException: Divide by zero
C
End of main
Handled: Divide by zero
DEnd of main
Attempts:
2 left
💡 Hint
Check how CoroutineExceptionHandler works with launch.
🔧 Debug
advanced
2:00remaining
Why does this coroutine exception crash the program?
Consider this Kotlin code snippet. Why does the program crash with an unhandled exception?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val job = async {
        throw IllegalStateException("Failure in async")
    }
    job.await()
    println("This line is never reached")
}
ABecause runBlocking does not handle exceptions from child coroutines.
BBecause async does not support exceptions and crashes immediately.
CBecause exceptions in async are thrown only when awaited, and await is not inside try-catch.
DBecause IllegalStateException is not a checked exception and must be caught explicitly.
Attempts:
2 left
💡 Hint
Think about when exceptions from async coroutines are delivered.
📝 Syntax
advanced
2:00remaining
Correct syntax for catch block in coroutine exception handling
Which option contains the correct syntax for the catch block in Kotlin coroutine exception handling?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val handler = CoroutineExceptionHandler { _, exception ->
        println("Caught: ${exception.message}")
    }
    val job = launch(handler) {
        try {
            throw Exception("Oops")
        } catch e: Exception {
            println("Handled inside try-catch")
        }
    }
    job.join()
}
Acatch e: Exception { println("Handled inside try-catch") }
Bcatch (e: Exception) println("Handled inside try-catch")
Ccatch { e: Exception -> println("Handled inside try-catch") }
Dcatch (e: Exception) { println("Handled inside try-catch") }
Attempts:
2 left
💡 Hint
The standard Kotlin syntax for catch is 'catch (e: Exception) { ... }'.
🚀 Application
expert
3:00remaining
How many exceptions are caught and printed?
Given this Kotlin coroutine code, how many exception messages will be printed?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val handler = CoroutineExceptionHandler { _, e ->
        println("Handler caught: ${e.message}")
    }
    val supervisor = SupervisorJob()
    val scope = CoroutineScope(coroutineContext + supervisor + handler)

    val job1 = scope.launch {
        throw RuntimeException("Job1 failed")
    }
    val job2 = scope.launch {
        try {
            throw IllegalArgumentException("Job2 failed")
        } catch (e: IllegalArgumentException) {
            println("Caught in job2: ${e.message}")
        }
    }
    joinAll(job1, job2)
    println("All jobs done")
}
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
Consider which exceptions are caught inside coroutines and which are handled by CoroutineExceptionHandler.