【问题标题】:rx-java2 exception handlingrx-java2 异常处理
【发布时间】:2018-06-21 12:58:32
【问题描述】:

我正在使用可流动的迭代器并处理来自可迭代的每个项目的请求。如果抛出任何异常,如何找出发生异常的输入。

例如:

Flowable.fromIterable(userList)
            .flatMap(d -> Flowable.fromCallable(() -> getClaimStatus(d))
            ).map(d -> updateClaimStatus(d))
            .subscribe(d -> System.out.println("Processed"),
                    err -> System.err.println(err.getMessage()));

我只想打印发生错误的用户。

【问题讨论】:

    标签: rx-java2


    【解决方案1】:

    您可以尝试在 fromCallable/map 中捕获并抛出您选择的包装异常:

    Flowable.fromIterable(userList)
        .flatMap(d -> Flowable.fromCallable(() -> {
           try {
               return getClaimStatus(d);
           } catch (Exception ex) {
               throw new Exception("User: " + d, ex);
           }
        }))
        .map(d -> {
            try {
                return updateClaimStatus(d);
           } catch (Exception ex) {
               throw new Exception("User: " + d, ex);
           }
        })
        .subscribe(
            d -> System.out.println("Processed"),
            err -> System.err.println(err.getMessage())
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多