0
0
Kotlinprogramming~10 mins

Flow exception handling 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 in a Flow using the catch operator.

Kotlin
flowOf(1, 2, 3)
    .[1] { e -> emit(-1) }
Drag options to blanks, or click blank then click option'
Acatch
Bcollect
Cmap
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using collect instead of catch to handle exceptions.
Using map or filter which do not handle exceptions.
2fill in blank
medium

Complete the code to emit a fallback value when an exception occurs in the Flow.

Kotlin
flow {
    emit(1)
    throw RuntimeException("Error")
}.catch { [1] -> emit(0) }
Drag options to blanks, or click blank then click option'
Ae
Bexception
Cerror
Dex
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that is not used in the lambda.
Forgetting to name the exception parameter.
3fill in blank
hard

Fix the error in the code to properly handle exceptions in the Flow.

Kotlin
flow {
    emit(1)
    throw Exception("Fail")
}.[1] { e -> emit(-1) }
Drag options to blanks, or click blank then click option'
AcatchError
BhandleException
ConError
Dcatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent operators like catchError or onError.
Trying to handle exceptions outside the Flow operators.
4fill in blank
hard

Fill both blanks to emit values and handle exceptions with catch.

Kotlin
flow {
    emit(10)
    emit(20)
    throw Exception("Oops")
}.[1] { e ->
    emit([2])
}
Drag options to blanks, or click blank then click option'
Acatch
Bcollect
C30
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using collect instead of catch for exception handling.
Emitting a value that does not indicate an error fallback.
5fill in blank
hard

Fill all three blanks to create a Flow that emits numbers, handles exceptions, and collects the results.

Kotlin
val flow = flow {
    emit(1)
    emit(2)
    throw Exception("Error")
}

flow.[1] { e ->
    emit([2])
}.[3] { value ->
    println(value)
}
Drag options to blanks, or click blank then click option'
Acatch
B0
Ccollect
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of catch for exception handling.
Forgetting to collect the Flow to trigger execution.
Emitting a wrong fallback value.