简体   繁体   English

如何捕获反应堆抛出的异常?

[英]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.

相关问题 如何处理Project Reactor中处理器订阅中抛出的异常 - How to handle exceptions thrown in subscriptions to processors in Project Reactor 如何捕获Spring Security Matchers抛出的异常? - How to catch the exceptions thrown by spring security matchers? @ControllerAdvice 不捕获抛出的异常 - @ControllerAdvice not catch the thrown exceptions @ExceptionHandler不会捕获从Spring Formatters抛出的异常 - @ExceptionHandler doesn't catch exceptions being thrown from Spring Formatters 捕获未在本地抛出的异常? - Catch exceptions which are not thrown locally? 如何使用Java代码捕获可执行文件运行抛出的异常? - How can 1 catch the exceptions thrown by the executable run using Java code? 如何在没有尝试捕获的情况下处理流 map 中引发的异常? - How to handle exceptions thrown in streams map without try catch? 如何使用Reactor Framework 2.x在多线程映射/归约中捕获/检测异常? - How to catch/detect exceptions in multi-threaded map/reduce using Reactor framework 2.x? 如何处理自定义的HandlerMethodArgumentResolver引发的异常? - How to handle exceptions thrown from customized HandlerMethodArgumentResolver? 如何处理Freemarker中从Java抛出的异常? - How to handle exceptions thrown from Java in Freemarker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM