Flow exception handling (Part 1)
No try/catch inside Flow!?
You may have heard that “No try/catch inside Flow” or “Only use catch() operator”. But why? Let’s explore from the beginning. Here is a very simple setup: fun main() = runBlocking { val worker = Worker() val scope = CoroutineScope(EmptyCoroutineContext) val flow = flow { emit(1) emit(2) emit(3) } scope.launch { try { flow.collect { println(worker.doWork(it)) } } catch (e: Exception) { println("Error in collect: ${e.message}") } } delay(10000) } class Worker { fun doWork(n: Int): String = if (Random.