1.操作符
- onErrorResumeNext
- onErrorReturn
- onExceptionResumeNext
2.操作符案例
2.1 onErrorResumeNext
說明
攔截髮射的異常 ,能夠處理 throw Exception級別的異常,能夠接收異常消息,需要返回一個新的Observable,執行發送 ,不能處理 throw Error級別異常
例子
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
emitter.onNext("1");
emitter.onNext("2");
emitter.onNext("3");
throw new RuntimeException("hahahhaa"); // 下游onErrorResumeNext 能夠處理
// throw new IllegalAccessError("hahahhaa"); //下游onErrorResumeNext 不能處理
}
})
.onErrorResumeNext(new Function<Throwable, ObservableSource<? extends String>>() {
@Override
public ObservableSource<? extends String> apply(Throwable throwable) throws Exception {
throwable.printStackTrace();
return Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
emitter.onNext("4");
emitter.onNext("5");
emitter.onNext("6");
}
});
}
})
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
Log.i(TAG, "onNext: " + s);
}
@Override
public void onError(Throwable e) {
Log.i(TAG, "onError: " + e);
}
@Override
public void onComplete() {
Log.i(TAG, "onComplete: ");
}
});
輸出信息 (發送完成以後 手動拋出異常,能夠被onErrorResumeNext 捕獲,並且在裏面創建了一個新的Observable,繼續發送)
10-25 05:50:17.772 I/MainActivity: onNext: 1
10-25 05:50:17.772 I/MainActivity: onNext: 2
10-25 05:50:17.772 I/MainActivity: onNext: 3
10-25 05:50:17.772 W/System.err: java.lang.RuntimeException: hahahhaa
10-25 05:50:17.772 W/System.err: at com.xiaxiayige.rxjavademo.MainActivity$36.subscribe(MainActivity.java:719)
10-25 05:50:17.772 W/System.err: at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
10-25 05:50:17.772 W/System.err: at io.reactivex.Observable.subscribe(Observable.java:12267)
10-25 05:50:17.772 W/System.err: at io.reactivex.internal.operators.observable.ObservableOnErrorNext.subscribeActual(ObservableOnErrorNext.java:38)
10-25 05:50:17.772 W/System.err: at io.reactivex.Observable.subscribe(Observable.java:12267)
10-25 05:50:17.772 W/System.err: at com.xiaxiayige.rxjavademo.MainActivity.aa24(MainActivity.java:738)
10-25 05:50:17.772 W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
10-25 05:50:17.772 W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
10-25 05:50:17.772 W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
10-25 05:50:17.772 W/System.err: at android.view.View.performClick(View.java:4204)
10-25 05:50:17.772 W/System.err: at android.view.View$PerformClick.run(View.java:17355)
10-25 05:50:17.772 W/System.err: at android.os.Handler.handleCallback(Handler.java:725)
10-25 05:50:17.772 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 05:50:17.772 W/System.err: at android.os.Looper.loop(Looper.java:137)
10-25 05:50:17.772 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5041)
10-25 05:50:17.772 W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
10-25 05:50:17.772 W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
10-25 05:50:17.772 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-25 05:50:17.772 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-25 05:50:17.772 W/System.err: at dalvik.system.NativeStart.main(Native Method)
10-25 05:50:17.772 I/MainActivonErrorResumeNextity: onNext: 4
10-25 05:50:17.772 I/MainActivity: onNext: 5
10-25 05:50:17.772 I/MainActivity: onNext: 6
2.2 onErrorReturn
說明
攔截髮射的異常,能夠處理 throw Execption級別異常 ,能夠接收異常消息,可以提供返回一個默認值,但是不能處理 throw Error級別異常
例子
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
emitter.onNext("1");
emitter.onNext("2");
emitter.onNext("3");
throw new RuntimeException("hahahhaa"); // 下游onErrorReturn 能夠處理
// throw new IllegalAccessError("hahahhaa"); //下游onErrorResumeNext 不能處理
}
})
.onErrorReturn(new Function<Throwable, String>() {
@Override
public String apply(Throwable throwable) throws Exception {
throwable.printStackTrace();
return "default";
}
})
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
Log.i(TAG, "onNext: " + s);
}
@Override
public void onError(Throwable e) {
Log.i(TAG, "onError: " + e);
}
@Override
public void onComplete() {
Log.i(TAG, "onComplete: ");
}
});
輸出結果
10-25 05:56:24.220 I/MainActivity: onNext: 1
10-25 05:56:24.220 I/MainActivity: onNext: 2
10-25 05:56:24.220 I/MainActivity: onNext: 3
10-25 05:56:24.220 W/System.err: java.lang.RuntimeException: hahahhaa
10-25 05:56:24.224 W/System.err: at com.xiaxiayige.rxjavademo.MainActivity$39.subscribe(MainActivity.java:769)
10-25 05:56:24.224 W/System.err: at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
10-25 05:56:24.224 W/System.err: at io.reactivex.Observable.subscribe(Observable.java:12267)
10-25 05:56:24.224 W/System.err: at io.reactivex.internal.operators.observable.ObservableOnErrorReturn.subscribeActual(ObservableOnErrorReturn.java:31)
10-25 05:56:24.224 W/System.err: at io.reactivex.Observable.subscribe(Observable.java:12267)
10-25 05:56:24.224 W/System.err: at com.xiaxiayige.rxjavademo.MainActivity.aa25(MainActivity.java:780)
10-25 05:56:24.224 W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
10-25 05:56:24.224 W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
10-25 05:56:24.224 W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
10-25 05:56:24.224 W/System.err: at android.view.View.performClick(View.java:4204)
10-25 05:56:24.224 W/System.err: at android.view.View$PerformClick.run(View.java:17355)
10-25 05:56:24.228 W/System.err: at android.os.Handler.handleCallback(Handler.java:725)
10-25 05:56:24.228 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 05:56:24.228 W/System.err: at android.os.Looper.loop(Looper.java:137)
10-25 05:56:24.228 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5041)
10-25 05:56:24.228 W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
10-25 05:56:24.228 W/System.err: at java.lang.reflect.Method.invoke(Method.java:511)
10-25 05:56:24.228 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-25 05:56:24.228 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-25 05:56:24.228 W/System.err: at dalvik.system.NativeStart.main(Native Method)
10-25 05:56:24.228 I/MainActivity: onNext: default
10-25 05:56:24.228 I/MainActivity: onComplete:
2.3 onExceptionResumeNext
說明
攔截髮射的異常,能夠處理 throw Execption級別異常 不接受異常信息,獲得Observer繼續發送,同樣不能處理 Error級別的異常
例子
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
emitter.onNext("1");
emitter.onNext("2");
emitter.onNext("3");
throw new RuntimeException("hahahhaa"); // 下游onErrorReturn 能夠處理
// throw new IllegalAccessError("hahahhaa"); //下游onErrorResumeNext 不能處理
}
})
.onExceptionResumeNext(new ObservableSource<String>() {
@Override
public void subscribe(Observer<? super String> observer) {
observer.onNext("4");
observer.onNext("5");
observer.onNext("6");
}
})
.subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String s) {
Log.i(TAG, "onNext: " + s);
}
@Override
public void onError(Throwable e) {
Log.i(TAG, "onError: " + e);
}
@Override
public void onComplete() {
Log.i(TAG, "onComplete: ");
}
});
輸出結果 這裏拋出了異常,但是接收的地方並沒有告知是什麼異常,我們可以不用處理。正常執行我們的操作即可
10-25 05:54:48.004 I/MainActivity: onNext: 1
10-25 05:54:48.004 I/MainActivity: onNext: 2
10-25 05:54:48.004 I/MainActivity: onNext: 3
10-25 05:54:48.004 I/MainActivity: onNext: 4
10-25 05:54:48.004 I/MainActivity: onNext: 5
10-25 05:54:48.004 I/MainActivity: onNext: 6
3.總結對比
1. 都不能處理Error級別的錯誤,
2. 都能攔截到上游的異常
onErrorResumeNext 攔截髮射的異常 ,能夠處理 throw Exception級別的異常,能夠接收異常消息,需要返回一個新的Observable,執行發送 ,不能處理 throw Error級別異常
onErrorReturn 攔截髮射的異常,能夠處理 throw Execption級別異常 ,能夠接收異常消息,可以提供返回一個默認值,但是不能處理 throw Error級別異常
onExceptionResumeNext 攔截髮射的異常,能夠處理 throw Execption級別異常 不接受異常信息,獲得Observer繼續發送,同樣不能處理 Error級別的異常