question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to throw an exception from within an Observer

See original GitHub issue

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 ?

Issue Analytics

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

github_iconTop GitHub Comments

10reactions
Dorvaryncommented, Mar 19, 2014

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.

7reactions
Dorvaryncommented, Mar 19, 2014

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What if an exception is thrown by the observer in OnNext?
Hi Flavien, If an error is thrown by an observer it appears to go unhandled. This is the correct behavior, IMO. Most likely...
Read more >
Laravel nova, throw exception in Observer - Laracasts
Hi,. How can I throw exception in an Observer, so the error will show in Nova? This works, but no errors/exception is thrown:...
Read more >
How to handle exceptions thrown by observer's onNext in ...
How to handle exceptions thrown by observer's onNext in RxJava? ... Consider the following example: Observable.range(1, 10).subscribe(i -> { System.out.println(i); ...
Read more >
Observers Should Never Throw Exceptions - C2 wiki
The main issue is what happens when an Observer is coded incorrectly and throws a RuntimeException. If the Observable is not coded defensively...
Read more >
How to Throw an Exception from Event Observer in Magento2
First, we need to declare our Event observer by creating “events.xml” file at this path in our extension folder. app\code\Vendor\Extension\etc\ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found