[英]How to catch exceptions thrown from reactor?
I want to catch exceptions thrown from a flux, my code is like this:我想捕获从通量抛出的异常,我的代码是这样的:
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
}
the output is: 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)
...
It seems that my catch at step 3 is never reached.似乎从未达到我在第 3 步的捕获量。
I know I can reach the exception at step 2 like this: .subscribe(System.out::println, e -> System.out.println("Got Exception"))
.我知道我可以像这样在第 2 步遇到异常: .subscribe(System.out::println, e -> System.out.println("Got Exception"))
。
But how can I catch the exception thrown at step 1 out of the flux?但是我怎样才能从通量中捕捉到第 1 步抛出的异常呢?
You can use the onError()
operator to handle error cases, or the doOnError()
operator if you eg want to log the exception.您可以使用onError()
运算符来处理错误情况,或者如果您想记录异常,则可以使用doOnError()
运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.