🐦 Kotlin
Advanced
What is Flow.catch in Kotlin?
Answer
Flow.catch is an intermediate operator that catches exceptions thrown by the upstream flow and allows you to handle them — either by emitting a fallback value or logging the error. flow.catch { e -> emit(emptyList()) }.collect { }. Critically, catch only catches exceptions from the upstream — it does not catch exceptions thrown in the collector itself. For that, wrap the collect call in a try-catch. Combining catch with onEach and operators like retry gives you a complete error handling strategy for production flow pipelines.
Previous
What is the contract in Kotlin?
Next
What is the difference between hot and cold flows in Kotlin?