0
0
Kotlinprogramming~20 mins

Flow exception handling in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flow Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin Flow exception handling code?

Consider the following Kotlin code using flow and catch. What will be printed?

Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    flow {
        emit(1)
        emit(2)
        throw RuntimeException("Error in flow")
    }
    .catch { e -> emit(-1) }
    .collect { value -> println(value) }
}
A
1
2
B
1
2
-1
C
1
-1
D
1
2
Exception in thread "main" java.lang.RuntimeException: Error in flow
Attempts:
2 left
💡 Hint

The catch operator catches exceptions and can emit fallback values.

Predict Output
intermediate
2:00remaining
What error does this Kotlin Flow code raise?

What error will this Kotlin code produce when run?

Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    flow {
        emit(1)
        throw IllegalStateException("Oops")
    }
    .collect { value ->
        if (value == 1) throw ArithmeticException("Error in collector")
        println(value)
    }
}
AIllegalStateException: Oops
BNullPointerException
CNo error, prints 1
DArithmeticException: Error in collector
Attempts:
2 left
💡 Hint

Exceptions thrown inside collect are not caught by catch in the flow.

🧠 Conceptual
advanced
1:30remaining
Which operator handles exceptions inside the flow builder?

In Kotlin Flow, which operator is designed to catch exceptions thrown inside the flow builder block?

A<code>catch</code>
B<code>onCompletion</code>
C<code>retry</code>
D<code>collect</code>
Attempts:
2 left
💡 Hint

Think about which operator intercepts exceptions during emission.

Predict Output
advanced
2:00remaining
What is the output of this Kotlin Flow with retry?

What will this Kotlin program print?

Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

var attempt = 0

fun main() = runBlocking {
    flow {
        attempt++
        if (attempt < 3) throw Exception("Try again")
        emit("Success on attempt $attempt")
    }
    .retry(2)
    .catch { e -> emit("Failed after retries") }
    .collect { println(it) }
}
ASuccess on attempt 3
BFailed after retries
CException in thread "main" java.lang.Exception: Try again
DSuccess on attempt 1
Attempts:
2 left
💡 Hint

The retry operator retries the flow emission on exceptions.

🔧 Debug
expert
2:30remaining
Why does this Kotlin Flow catch block not catch the exception?

Examine this Kotlin code. Why does the catch block not catch the exception?

Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    flow {
        emit(1)
        emit(2)
    }
    .catch { e -> println("Caught: $e") }
    .collect { value ->
        if (value == 2) throw RuntimeException("Error in collector")
        println(value)
    }
}
ABecause the flow does not throw any exception
BBecause the catch operator must be before collect to catch exceptions
CBecause exceptions in the collector are not caught by the flow's catch operator
DBecause RuntimeException is not caught by catch
Attempts:
2 left
💡 Hint

Consider where exceptions are thrown and where catch applies.