onNext中未捕获的异常将传播到RxJava全局异常处理程序

时间:2018-12-03 10:21:27

标签: java android observable rx-java rx-java2

以下是我要解决的问题的简化示例。在我的代码库中,PublishSubject出现了问题,但是可以使用Observable重现此问题。

问题:

订户的onNext()中有一个未捕获的异常,该异常将异常传播到RxJava的全局异常处理程序,从而导致崩溃。但是,这不是所需的行为。目的是优雅地处理未捕获的异常。

问题:

为什么没有为未捕获的异常调用onError()?我可以使用什么方法来捕获此类未捕获的异常?

快速解决方案:

我可以使用RxJavaPlugins.setErrorHandler来解决这种情况,但是如果我们没有其他解决方案可以解决此问题,我宁愿将此解决方案作为最后的选择。

代码:

Observable.just(true)
                .subscribeWith(new DisposableObserver<Boolean>() {
                    @Override
                    public void onNext(Boolean aBoolean) {
                        String x = null;
                        x.getBytes();
                    }

                    @Override
                    public void onError(Throwable e) { <--- NOT CALLED UPON EXCEPTION
                        System.out.println("inside test observable onError");
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

堆栈跟踪:

12-03 11:09:01.454 4750-4750/? E/MDMApplication: UNCAUGHT EXCEPTION IN APP: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.mdm.app/mdm.connector.app.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at mdm.connector.app.MainActivity$1.onNext(MainActivity.java:63)
        at mdm.connector.app.MainActivity$1.onNext(MainActivity.java:59)
        at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onNext(ObservableDoOnEach.java:103)
        at io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:64)
        at io.reactivex.internal.operators.observable.ObservableScalarXMap$ScalarDisposable.run(ObservableScalarXMap.java:248)
        at io.reactivex.internal.operators.observable.ObservableJust.subscribeActual(ObservableJust.java:35)
        at io.reactivex.Observable.subscribe(Observable.java:12036)
        at io.reactivex.internal.operators.observable.ObservableMap.subscribeActual(ObservableMap.java:33)
        at io.reactivex.Observable.subscribe(Observable.java:12036)
        at io.reactivex.internal.operators.observable.ObservableDoOnEach.subscribeActual(ObservableDoOnEach.java:42)
        at io.reactivex.Observable.subscribe(Observable.java:12036)
        at io.reactivex.Observable.subscribeWith(Observable.java:12088)
        at mdm.connector.app.MainActivity.onCreate(MainActivity.java:59)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
        at android.app.ActivityThread.access$800(ActivityThread.java:135) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5017) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
        at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

上述问题的解决方案是,应在subscribe方法中使用Consumer&Consumer,而不是使用DisposableObserver。这样可以确保如果Consumer接受方法中有任何未捕获的异常,它们将被捕获到Consumer接受方法中,而不是传播到RxJava的全局异常处理程序中并导致崩溃。

示例:

Observable.just(true)
.subscribe(new Consumer<Boolean> {
  public void accept(Boolean aboolean) {
      String x = null;
      x.getBytes();
  }
}, new Consumer<Throwable> {

  public void accept(Throwable e) {
      e.printStackTrace()
  }
});

如果还有其他更好的解决方案来处理未处理的异常,请在评论中建议