Challenge - 5 Problems
Coroutine Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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") }
Attempts:
2 left
💡 Hint
Look at where the exception is caught inside the coroutine.
✗ Incorrect
The exception is thrown inside the coroutine but caught by the try-catch block inside the coroutine itself, so the message is printed first. Then after the coroutine finishes, the main prints "Coroutine finished".
❓ Predict Output
intermediate2: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") }
Attempts:
2 left
💡 Hint
Check how CoroutineExceptionHandler works with launch.
✗ Incorrect
The CoroutineExceptionHandler catches the exception thrown in the launch coroutine and prints the message. Then the main continues and prints "End of main".
🔧 Debug
advanced2: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") }
Attempts:
2 left
💡 Hint
Think about when exceptions from async coroutines are delivered.
✗ Incorrect
Exceptions in async coroutines are deferred until await is called. Since await is called without try-catch, the exception propagates and crashes the program.
📝 Syntax
advanced2: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() }
Attempts:
2 left
💡 Hint
The standard Kotlin syntax for catch is 'catch (e: Exception) { ... }'.
✗ Incorrect
B is the correct syntax. In Kotlin, catch blocks require parentheses around the exception parameter: catch (e: Exception) { ... }. Option D misses parentheses, C misses braces for the block, D has wrong syntax.
🚀 Application
expert3: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") }
Attempts:
2 left
💡 Hint
Consider which exceptions are caught inside coroutines and which are handled by CoroutineExceptionHandler.
✗ Incorrect
Job1 throws an exception not caught inside, so handler prints its message. Job2 catches its own exception and prints it. So two messages are printed before "All jobs done".