Predict the output
What does this print?
fun main() = runBlocking {
flow {
emit(1)
throw IllegalStateException("boom")
}
.catch { emit(-1) }
.onCompletion { println("done") }
.collect { println(it) }
}Hint 1 of 1
Which way does catch look: up or down?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 1, -1, done
Correct. catch handles the upstream error and emits a fallback, then the flow completes.
Why not the others?
- B 1, then it crashes. catch is upstream of collect, so the error is handled.
- C -1, done. 1 is emitted before the throw.
- D 1, done. catch emits -1 before completion.