簡體   English   中英

如何捕獲反應堆拋出的異常?

[英]How to catch exceptions thrown from reactor?

我想捕獲從通量拋出的異常,我的代碼是這樣的:

        try {
            Flux.just("key1", "key2", "key3")
                    .doOnNext(System.out::println)
                    .map(k -> {
                        if (!k.equals("key1")) {
                            throw new RuntimeException("Not key1"); // 1
                        }
                        return "External Value, key:" + k;
                    })
                    .subscribe(System.out::println); // 2
        } catch (Throwable e) {
            System.out.println("Got exception"); // 3
        }

output 是:

key1
External Value, key:key1
key2
[ERROR] (main) Operator called default onErrorDropped - reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.RuntimeException: Not key1
reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.RuntimeException: Not key1
Caused by: java.lang.RuntimeException: Not key1
    at com.cxd.study.reactor.HandlingErrors.lambda$catchException$7(HandlingErrors.java:153)
...

似乎從未達到我在第 3 步的捕獲量

我知道我可以像這樣在第 2 步遇到異常: .subscribe(System.out::println, e -> System.out.println("Got Exception"))

但是我怎樣才能從通量中捕捉到第 1 步拋出的異常呢?

您可以使用onError()運算符來處理錯誤情況,或者如果您想記錄異常,則可以使用doOnError()運算符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM