0
0
Kotlinprogramming~10 mins

CoroutineExceptionHandler 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 create a CoroutineExceptionHandler that prints the exception message.

Kotlin
val handler = CoroutineExceptionHandler { _, exception ->
    println("Caught: " + [1].message)
}
Drag options to blanks, or click blank then click option'
Ait
Bexception
Cerror
Dthrowable
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parameter name like 'it' or 'error' which are not defined here.
2fill in blank
medium

Complete the code to launch a coroutine with the exception handler attached.

Kotlin
GlobalScope.launch([1]) {
    throw RuntimeException("Error!")
}
Drag options to blanks, or click blank then click option'
Ahandler
BDispatchers.IO
CDispatchers.Main
DSupervisorJob()
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a dispatcher instead of the exception handler.
3fill in blank
hard

Fix the error in the CoroutineExceptionHandler usage to properly catch exceptions.

Kotlin
val handler = CoroutineExceptionHandler { _, [1] ->
    println("Caught: " + exception.message)
}
Drag options to blanks, or click blank then click option'
Aexception
Berror
Cthrowable
De
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name but referencing 'exception' inside the block.
4fill in blank
hard

Fill both blanks to create a CoroutineScope with a SupervisorJob and the exception handler.

Kotlin
val scope = CoroutineScope([1] + [2])
Drag options to blanks, or click blank then click option'
ASupervisorJob()
Bhandler
CDispatchers.Default
DJob()
Attempts:
3 left
💡 Hint
Common Mistakes
Using Job() instead of SupervisorJob().
Not including the exception handler.
5fill in blank
hard

Fill all three blanks to define a CoroutineExceptionHandler that logs the error, then launch a coroutine with it and a dispatcher.

Kotlin
val handler = CoroutineExceptionHandler { _, [1] ->
    println("Error: " + [2].message)
}

GlobalScope.launch([3] + handler) {
    throw IllegalStateException("Oops")
}
Drag options to blanks, or click blank then click option'
Aexception
CDispatchers.Default
DDispatchers.IO
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the exception parameter and inside the block.
Omitting the dispatcher or using the wrong one.