0
0
Kotlinprogramming~10 mins

Exception handling in coroutines in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to catch exceptions inside a coroutine.

Kotlin
launch {
    try {
        // Some suspending function
        fetchData()
    } catch ([1]: Exception) {
        println("Error caught")
    }
}
Drag options to blanks, or click blank then click option'
Ae
Berror
Cex
Dexception
Attempts:
3 left
💡 Hint
Common Mistakes
Using a keyword or invalid variable name in catch block.
Not providing a variable name after catch.
2fill in blank
medium

Complete the code to handle exceptions globally in a CoroutineScope.

Kotlin
val handler = CoroutineExceptionHandler { _, [1] ->
    println("Caught $[1]")
}

CoroutineScope(Dispatchers.Default + handler).launch {
    throw RuntimeException("Fail")
}
Drag options to blanks, or click blank then click option'
Athrowable
Berror
Ce
Dexception
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names that don't match the handler signature.
Confusing the exception variable with the context.
3fill in blank
hard

Fix the error in the coroutine exception handling code.

Kotlin
val handler = CoroutineExceptionHandler { _, throwable ->
    println("Caught: $[1]")
}

GlobalScope.launch(handler) {
    throw Exception("Oops")
}
Drag options to blanks, or click blank then click option'
Aerror
Bthrowable
Ce
Dex
Attempts:
3 left
💡 Hint
Common Mistakes
Referencing a variable name not declared in the lambda parameters.
Using a different variable name inside the lambda body.
4fill in blank
hard

Fill both blanks to create a map of word lengths filtering words longer than 3 characters.

Kotlin
val words = listOf("cat", "house", "dog", "elephant")
val lengths = words.associateWith { it.[1] }
    .filter { it.value [2] 3 }
Drag options to blanks, or click blank then click option'
Alength
B>
C<
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using count without parentheses which is a function.
Using wrong comparison operators like < instead of >.
5fill in blank
hard

Fill all three blanks to create a map of uppercase keys and values filtered by positive values.

Kotlin
val data = mapOf("a" to 1, "b" to -2, "c" to 3)
val result = data.mapKeys { it.key.[1]() }
    .filter { it.value [2] 0 }
    .mapValues { it.[3] }
Drag options to blanks, or click blank then click option'
AtoUpperCase
B>
Cvalue
DtoLowerCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using toLowerCase() instead of uppercase.
Filtering with wrong comparison operator.
Using key instead of value in mapValues.