How to throw an exception from within an Observer
See original GitHub issueHi,
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:
- Created 10 years ago
- Comments:29 (23 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.