RxJava: How to throw an exception from within an Observer

Hi,

I am trying to make the app fail fast when something is going wrong from the Observer. I was surprised to see that the Exception was swallowed by RxJava.

Here is a sample I tried :


 Observable.error(new Exception()).subscribe(new Observer<Object>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        throw new IllegalStateException("This should crash the app");
                    }

                    @Override
                    public void onNext(Object o) {

                    }
                });

Do I need do do anything specific to fail from within an Observer ?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 29 (23 by maintainers)

Commits related to this issue

Most upvoted comments

I get why catching all errors from within the flow of RxJava (ie observables, functions etc…) to keep the monadic properties but once you reach the Observer the flow is consumed and this the point in the app where you can act on the events. The idea here is to fail fast in order to identify easily developer errors during the development phase. I was under the assumption after reading this closed issue https://github.com/Netflix/RxJava/issues/650 that RxJava should not be swallowing exceptions from within an Observer.

I understand that onError is called only once, I’m not trying to forward the exception to something else. What I want to do is be able to throw a RuntimeException to make the app terminate.

Sorry for reviving an old discussion again, but can someone help me?

If the user provided Subscriber.onError throws an exception, it will always be treated as fatal (since onError itself failed).

Is this still viable for RxJava 2?