小编Gre*_*reg的帖子

带有 catch 运算符的 Kotlin Flow 仍能完成

我无法理解catch运算符在 kotlin 中的工作原理Flow。 这是捕获文档

问题:

  1. 为什么 a 的存在不允许catch在Flow遇到异常时继续,而不是完成?
  2. 操作员的放置catch似乎改变了行为。为什么我不能将catch运算符放在链的末尾以看到相同的结果?在我的示例中,只有当我将其放置在 BEFORE 之前,它才会执行onEach。

示例要点

第一个例子,放置catchBEFORE onEach:

fun main() {
    // Flow of lambdas that return a String (or throw an Exception)
    flowOf<() -> String>({ "Hello " }, { error("error") }, { "World" })
        // Map to the result of the invocation of the lambda
        .map { it() }
        // This line will emit the error …
Run Code Online (Sandbox Code Playgroud)

kotlin kotlin-coroutines kotlin-flow

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

kotlin ×1

kotlin-coroutines ×1

kotlin-flow ×1