Rx.exceptions.OnErrorNotImplementedException How to avoid this error - its crashing my application

com.myapp.test.debug E / MessageQueue-JNI: rx.exceptions.OnErrorNotImplementedException in rx.Observable $ 31.onError (Observable.java:7134) in rx.observers.SafeSubscriber._onError (SafeSubscriber.r). observers.SafeSubscriber.onError (SafeSubscriber.java:111) in rx.observers.SafeSubscriber.onNext (SafeSubscriber.java:137) in rx.subjects.SubjectSubscriptionManager $ SubjectObserver.onNext (SubjectSubsubsubslish.subsubslish.subsubslish.subsubslish.subslishSubslish.subsubslish.subslishSubslishSubslishSubsubslishSubslishSubslishSubslishSubslishSubslishSubslishMubager .onNext (PublishSubject.java:121) at com.myapp.MyClass.lambda $ static $ 53 (MyClass.java:77) where MyClass.java 77 MySubject.onNext (event);

A few questions ... Why is the actual error also not shown in addition to an error handler that is not implemented? Secondly, where do I expect to implement this function on my topic? How exactly should I add an error handler to the topic. Thank you, this is a failure of my application and the actual error is not even shown. Think I should always execute an error handler?

+5
source share
2 answers

RxJava has Observable and Observer . You can view Observable as the source of your stream, on which you can perform operations such as map and filter . Observer is a kind of receiver : it is an interface with three methods ( onNext , onError and onCompleted ) that are launched using the Observable . You connect Observable and Observer using the methods Observable.subscribe(...) .

There are several subscribe overloads that allow you to provide onNext , onError and onCompleted as separate functions. These functions are then used to implement the Observer interface. If you do not provide all three functions (say, only onNext ), the onError method of the Observer interface is implemented by throwing an OnErrorNotImplementedException .

Your code supposedly looks something like this.

 PublishSubject<Integer> subject = PublishSubject.create(); subject.subscribe(System.out::println); // I use a Java 8 lambda here for brevity subject.onNext(1/0); // this causes an error for dividing by 0 

You can catch this exception by not only providing an onNext implementation in subscribe , but also by providing an onError implementation:

 PublishSubject<Integer> subject = PublishSubject.create(); subject.subscribe(System.out::println, Throwable::printStacktrace); subject.onNext(1/0); 

Regarding your last question, “Should I always execute the onError function?”: Technically speaking no, you do not need it if you are sure that the Observable (or Subject ) will not produce an error. In practice, however, it is a reasonable idea to at least register such an error or even repair it using an operator like onErrorResumeNext or retry . You can find everything about them in the documentation .

+4
source

Be aware of any signatures that may cause errors. For example, I have a subscription executing a statement that returned true the first time, but since the next value made the statement throw an error, then my observable was caught with this exception error.

0
source

Source: https://habr.com/ru/post/1247408/


All Articles