a list of error-handling operators
Used to respond to observable launch onError notifications or to recover from errors, for example, you
Can: Swallow this error, switch to a standby observable continue to launch data swallow this error and then launch the default value swallow this error and immediately try to reboot this observable to swallow this error and reboot after some fallback interval observable
name |
parsing |
Onerrorresumenext () |
Indicates that observable emits a sequence of data when an error is encountered |
Onerrorreturn () |
Allow observable to launch a special item and terminate normally when an error is encountered. method returns a new observable that mirrors the original observable behavior, which ignores the onerror invocation of the former and does not pass the error to the Observer as an alternative, sending a special item and invoking the Observer's OnCompleted method. |
Onexceptionresumenext () |
Indicates that observable continues to emit data when an error is encountered |
Retry () |
Indicates that observable is trying again when an error is encountered |
Retrywhen () |
Indicates that when observable encounters an error, it passes the error to another observable to decide whether or not to subscribe to this observable |
Retryuntil () |
Indicates whether observable has observable to subscribe when an error is encountered |
second, error handling operators
2.1 Onerrorreturn operator
Allow observable to emit a special item when it encounters an error and terminate normally, Onerrorretrun can catch the exception that occurred before it, and it will not be able to control the exception that occurs after the operation in the stream.
public void Testonerrorreturn () {observable.create () {new observableonsubscribe<string> () {
@Override public void Subscribe (observableemitter<string> e) throws Exception { for (int i = 0; i<= 3; i++) {if (i = = 2) {E.one
Rror (New Throwable ("error occurred"));
}else{E.onnext (i+ "");
} try{thread.sleep (1000);
}catch (Exception ex) {ex.printstacktrace ();
} e.oncomplete (); }). Subscribeon (Schedulers.newthread ()). Onerrorreturn (New Function<t
Hrowable, string> () {@Override Public String Apply (@NonNull throwable throwable) throws Exception {log.e (TAG, "in one
Rrorreturn dealt with: "+throwable.tostring ());
After intercepting the error, return a result launch, and then the normal end.
return "1";
}). Subscribe (New consumer<string> () {@Override
public void Accept (@NonNull String s) throws Exception {log.e (TAG, "received the message:" + s); }, New Consumer<throwable> () {@Override Publ IC void Accept (@NonNull throwable throwable) throws Exception {log.e (TAG, "Result error:" + throwable.to
String ());
}
}); }
The above code output is:
02-20 19:18:40.761 13445-13461/? E/erroractivity: Message Received: 0
02-20 19:18:41.762 13445-13461/cn.com.minstone.rxjavalearn e/erroractivity: Received message: 1
02-20 19:18:42.763 13445-13461/cn.com.minstone.rxjavalearn e/erroractivity: In Onerrorreturn processed: java.lang.Throwable: There was an error.
02-20 19:18:42.763 13445-13461/cn.com.minstone.rxjavalearn e/erroractivity: Message Received: 1
2.2 Onerrorresumenext operator
Unlike Onerrornext, Onerrorresumenext is returning a redefined observable,onerrornext that returns the data format of the launch.
PS: note Onerrorresumenext intercept error is throwable, can not intercept exception. Otherwise it would pass the error to the Observer's OnError method. To intercept exception, please use Onexceptionresumenext.
Here's a chestnut:
public void Testonerrorresumereturn () {observable.create () {new observableonsubscribe<string> () {
@Override public void Subscribe (observableemitter<string> e) throws Exception {
for (int i = 0; i<= 3; i++) {if (i = = 2) {//This is Throwable
E.onerror (New Throwable ("error occurred"));
}else{E.onnext (i+ "");
} try{thread.sleep (1000);
}catch (Exception ex) {ex.printstacktrace ();
} e.oncomplete (); }). Subscribeon (Schedulers.newthread ()). Onerrorresumenext (New Function<t Hrowable, observablesource<?
Extends String>> () { @Override public observablesource<?
Extends string> apply (@NonNull throwable throwable) throws Exception {//after blocking to the error, redefine the observed
return Observable.just ("redefined the observed");
}). Subscribe (New consumer<string> () {@Override
public void Accept (@NonNull String s) throws Exception {log.e (TAG, "received the message:" + s); }, New Consumer<throwable> () {@Override Publ IC void Accept (@NonNull throwable throwable) throws Exception {log.e (TAG, "Result error:" + throwable.to
String ());
}
});
}
The output is:
02-21 09:11:08.691 25682-25697/cn.com.minstone.rxjavalearn e/erroractivity: Message Received: 0
02-21 09:11:09.692 25682-25697/cn.com.minstone.rxjavalearn e/erroractivity: Message Received: 1
02-21 09:11:10.693 25682-25697/ Cn.com.minstone.rxjavalearn e/erroractivity: Received message: redefined the observed
2.3 Onexceptionresumenext operator
Onexceptionresumenext and Onerrorresumenext are basically the same, and are also receiving errors to redefine the new observer. But there is no need: if Onerrorresumenext received Throwable is not a exception, it will pass the error to the Observer's OnError method, Onexceptionresumenext will continue to intercept
PS: note Onexceptionresumenext intercept error is exception, can not intercept throwable. Otherwise it would pass the error to the Observer's OnError method. To intercept throwable, please use Onerrorresumenext.
public void Testonexceptionresumereturn () {observable.create () {new observableonsubscribe<string> () {
@Override public void Subscribe (observableemitter<string> e) throws Exception {
for (int i = 0; i<= 3; i++) {if (i = = 2) {//note this is exception
E.onerror (New Exception ("error occurred"));
}else{E.onnext (i+ "");
} try{thread.sleep (1000);
}catch (Exception ex) {ex.printstacktrace ();
} e.oncomplete (); }). Subscribeon (Schedulers.newthread ()). Onexceptionresumenext (New Observable< ; String> () {@Override protected void subscribeactual (OBSERVER<? Super String) > observer) {
Observer.onnext ("Error replacement message");
Observer.oncomplete ();
}). Subscribe (New consumer<string> () {@Override
public void Accept (@NonNull String s) throws Exception {log.e (TAG, "received the message:" + s); }, New Consumer<throwable> () {@Override Publ IC void Accept (@NonNull throwable throwable) throws Exception {log.e (TAG, "Result error:" + throwable.to
String ());
}
});
}
The result of the output is:
02-21 09:29:25.631 27412-27480/cn.com.minstone.rxjavalearn e/erroractivity: Message Received: 0
02-21 09:29:26.632 27412-27480/cn.com.minstone.rxjavalearn e/erroractivity: Message Received: 1
02-21 09:29:27.633 27412-27480/ Cn.com.minstone.rxjavalearn e/erroractivity: Message Received: Error replacement message
2.4 Retry operator
Retry, intercept the error, and then allow the observer to launch the data again. Throwable and exception can intercept.
It has five kinds of parameter methods:
- retry (): Allow the observer to resend the data, if the error has been sent
- retry (bipredicate): Interger is the first several times resend, Throwable is the wrong content
- retry (long time): Maximum number of times the observed person should launch data again
- retry (long time,predicate predicate): The maximum number of times the observed person will be sent the data again, in the predicate to determine whether the interception returned to continue
- retry (predicate predicate): in predicate to determine whether the interception returned to continue
public void Testretry () {observable.create (new observableonsubscribe<string> () {@Ov
Erride public void Subscribe (observableemitter<string> e) throws Exception { for (int i = 0; i<= 3; i++) {if (i = = 2) {E.onerror (new
Exception ("There is a mistake"));
}else{E.onnext (i+ "");
} try{thread.sleep (1000);
}catch (Exception ex) {ex.printstacktrace ();
} e.oncomplete (); }). Subscribeon (Schedulers.newthread ())/*.retry (New Predicate<throwa Ble> () {@Override public boOlean Test (@NonNull throwable throwable) throws Exception {log.e (TAG, "Retry error:" +throwable.tostr
ing ());
The return of the false is to not allow the data to be fired, the caller onerror of the observer is terminated.
Return true is to allow the observer to send the request back true; }) *//*.retry (new Bipredicate<integer, throwable> () {@Overrid
E public boolean test (@NonNull integer integer, @NonNull throwable throwable) throws Exception {
LOG.E (TAG, "Retry error:" +integer+ "" +throwable.tostring ());
The return of the false is to not allow the data to be fired, the caller onerror of the observer is terminated.
Return true is to allow the observer to send the request back true; ///. Retry (3)///////. The maximum number of times the observed person launches the data 3 time. Retry (3, New predicate<throwab Le> () {@Override public boolean test (@NonNull throwable throwAble) throws Exception {log.e (TAG, "Retry error:" +throwable.tostring ());
The maximum number of times that the observer can launch the data again is 3, but the return value here may be processed//return false is to not allow the data to be fired again, the caller's onerror is terminated.
Return true is to allow the observer to send the request back true;
}). Subscribe (New consumer<string> () {@Override
public void Accept (@NonNull String s) throws Exception {log.e (TAG, "received the message:" + s); }, New Consumer<throwable> () {@Override Publ IC void Accept (@NonNull throwable throwable) throws Exception {log.e (TAG, "Result error:" + throwable.to
String ());
}
}); }
2.5 retrywhen operator
Retrywhen and retry are similar, and the difference is that Retrywhen passes Throwable in onerror to a function that produces another observable, Retrywhen observes its results before deciding whether to subscribe to the original observable.
If the observable launches a data, it subscribes to it, and if the observable launches a ONERROR notification, it passes the notice to the observer and terminates.
PS: If the inside of the throwableobservable does not handle, then OnNext will intercept processing, there is a pit.
Chestnuts:
public void Testretrywhen () {observable.create () {new observableonsubscribe<string> () {
@Override public void Subscribe (observableemitter<string> e) throws Exception { for (int i = 0; I <= 3; i++) {if (i = = 2) {E.ON
Error (New Exception ("error occurred"));
else {E.onnext (i + "");
try {thread.sleep (1000);
catch (Exception ex) {ex.printstacktrace ();
} e.oncomplete (); }). Subscribeon (Schedulers.newthread ()). Retrywhen (New Function<obser Vable<throwable> Observablesource<?>> ({@Override public observablesource<?> apply (@NonNull Observable<throwa
Ble> throwableobservable) throws Exception {//Here you can send a new observed person observable
Return Throwableobservable.flatmap (New function<throwable, observablesource<?>> () { @Override public observablesource<?> Apply (@NonNull throwable throwable) throws Excep tion {//If the launch of the onerror will terminate return Observable.error (new Thro
Wable ("Retrywhen terminated"));
}
});
}). Subscribe (New consumer<string> () {@Override
public void Accept (@NonNull String s) throws Exception {log.e (TAG, "received the message:" + s); }, New ConsuMer<throwable> () {@Override public void accept (@NonNull throwable throwable
) throws Exception {log.e (TAG, "Result error:" + throwable.tostring ());
}
}); }
The output results are:
02-21 11:12:17.572 6471-6608/cn.com.minstone.rxjavalearn e/erroractivity: Message Received: 0
02-21 11:12:18.573 6471-6608/ Cn.com.minstone.rxjavalearn e/erroractivity: Message Received: 1
02-21 11:12:19.574 6471-6608/cn.com.minstone.rxjavalearn e/ Erroractivity: Wrong result: Java.lang.Throwable:retryWhen terminated.
2.5 retryuntil operator
RXJAVA2 operators.
Very simple, the function and retry (predicate) basically consistent, return really is not subscribe.