简体   繁体   English

RxJava2中的onErrorReturn仅捕获了第一个错误

[英]onErrorReturn in RxJava2 only caught the first error

I threw two exceptions in a flowable, and I used onErrorReturn to catch those exceptions. 我在可流动对象中抛出了两个异常,并使用onErrorReturn捕获了这些异常。 But I found that only the first exception was caught. 但是我发现只有第一个例外被捕获。 How to catch all the exceptions? 如何捕获所有异常?

Flowable.create(emitter -> {
    emitter.onError(new Exception("error1"));
    emitter.onError(new Exception("error2"));
    }, BackpressureStrategy.MISSING)
        .onErrorReturn(e -> {
            System.out.println("Got error " + e.getMessage());
            return "error";
        })
        .subscribe();

Output: 输出:

Got error error1
io.reactivex.exceptions.UndeliverableException: java.lang.Exception: error2
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:349)
    at io.reactivex.internal.operators.flowable.FlowableCreate$BaseEmitter.onError(FlowableCreate.java:271)
    at com.example.springboottest.SimpleApplication.lambda$main$0(SimpleApplication.java:48)
    at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:72)
    at io.reactivex.Flowable.subscribe(Flowable.java:13094)
    at io.reactivex.internal.operators.flowable.FlowableOnErrorReturn.subscribeActual(FlowableOnErrorReturn.java:33)
    at io.reactivex.Flowable.subscribe(Flowable.java:13094)
    at io.reactivex.Flowable.subscribe(Flowable.java:13030)
    at io.reactivex.Flowable.subscribe(Flowable.java:12890)
    at com.example.springboottest.SimpleApplication.main(SimpleApplication.java:54)
Caused by: java.lang.Exception: error2
    ... 8 more
Exception in thread "main" io.reactivex.exceptions.UndeliverableException: java.lang.Exception: error2
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:349)
    at io.reactivex.internal.operators.flowable.FlowableCreate$BaseEmitter.onError(FlowableCreate.java:271)
    at com.example.springboottest.SimpleApplication.lambda$main$0(SimpleApplication.java:48)
    at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:72)
    at io.reactivex.Flowable.subscribe(Flowable.java:13094)
    at io.reactivex.internal.operators.flowable.FlowableOnErrorReturn.subscribeActual(FlowableOnErrorReturn.java:33)
    at io.reactivex.Flowable.subscribe(Flowable.java:13094)
    at io.reactivex.Flowable.subscribe(Flowable.java:13030)
    at io.reactivex.Flowable.subscribe(Flowable.java:12890)
    at com.example.springboottest.SimpleApplication.main(SimpleApplication.java:54)
Caused by: java.lang.Exception: error2
    ... 8 more

An Observable ends after it completes or encounters an error. 一个Observable在完成或遇到错误后结束。 This is a part of the contract and there is no way around that property. 这是合同的一部分,没有办法解决该财产。

The only way you could handle 'multiple' exceptions in an observable is if it is handled like a value, not an exception. 您可以在可观察对象中处理“多个”异常的唯一方法是,是否像值一样处理它,而不是异常。 This would end up looking like Scala's Either construct in practice. 在实践中,这最终看起来像Scala的Either构造

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM