rx-java – rx. exceptions. Onerrornotimplementedexception how to avoid this error – it crashes my application

com. myapp. test. debug E / MessageQueue-JNI:rx. exceptions. OnErrorNotImplementedException

Some questions... Why hasn't the actual error been displayed except for the error handler? Secondly, where do I want to implement this function? How do I add an error handler to a topic Thank you for crashing my application without even showing the actual error I think I should always implement error handlers?

Solution

RX Java has an observable and an observer You can regard observable as the source of the stream, and you can perform operations such as map and filter on it Observer is a receiver: it is an interface of three methods (onnext, onerror and oncompleted) triggered by observable You can use observable The subscribe (...) method connects observable and observer

Subscriptions have multiple overloads, allowing you to provide onnext, onerror, and oncompleted as separate functions Then use these functions to implement the observer interface If you do not provide all three functions (such as onnext), implement the onerror method of the observer interface by throwing onerrornotimplementedexception

Maybe your code looks 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 providing not only the onnext implementation but also the onerror implementation in the subscription:

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

About your last question "should I always implement the onerror function?": Technically, no, if you are sure that observable (or subject) will not generate an error, you do not need to But in fact, it is a wise idea to at least record such errors and even recover from them using operators such as onerrorresumenext or retry You can find all of them in the documentation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>