Kotlin coroutines exception propagation

71 Views Asked by At

I would expect that throwing exception inside any flow will leads to propagation of this exception to coroutine collecting this flow. Nevertheless I'm experiencing quite a different behaviour. For instance considered the following example, which resembles problematic code quite closely:

try {
    channelFlow {
        send(1)
        error("random error")
    }.collect {
        println("$it")
    }
} catch (e: CancellationException) {
    println("Unexpected exception: ${e.cause}")
} catch (e: Exception) {
    println("Expected exception: $e")
}

In this example I'm getting Cancellation exception even when exception is thrown inside a channel flow. Is this a legit behaviour in some case?

0

There are 0 best solutions below