RxJava 2.x上的错误显示奇怪的堆栈跟踪

时间:2019-02-26 04:45:21

标签: java-8 observable rx-java2

我正在学习RxJava(这是绝对的新问题,很抱歉,如果这个问题太基础了),并且我在错误处理机制上遇到了困难(我已经阅读了文档,但是这对我来说是一种先进的方法)。

这是我的代码,

public static void main(String[] args) {
    Observable<String> source = Observable.just("Alpha", "Beta", "Gamma", "Upma", "Idly");
    Observer<String> myObserver = new Observer<String>() {
      @Override
      public void onSubscribe(Disposable d) {
        // do nothing with Disposable, disregard for now
      }
  @Override
  public void onNext(String value) {
    System.out.println("RECEIVED: " + value);
    throw new RuntimeException("I am thrown");
  }

  @Override
  public void onError(Throwable e) {
    System.out.println("I got an error !");
    e.printStackTrace(new PrintStream(System.out));
  }

  @Override
  public void onComplete() {
    System.out.println("Done!");
  }
};
   source.subscribe(myObserver);
  }

这是我的堆栈跟踪

RECEIVED: Alpha
io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.RuntimeException: I am thrown
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
    at io.reactivex.Observable.subscribe(Observable.java:12275)
    at reactivex.ReactMain.main(ReactMain.java:36)
Caused by: java.lang.RuntimeException: I am thrown
    at reactivex.ReactMain$1.onNext(ReactMain.java:22)
    at reactivex.ReactMain$1.onNext(ReactMain.java:1)
    at io.reactivex.internal.operators.observable.ObservableFromArray$FromArrayDisposable.run(ObservableFromArray.java:108)
    at io.reactivex.internal.operators.observable.ObservableFromArray.subscribeActual(ObservableFromArray.java:37)
    at io.reactivex.Observable.subscribe(Observable.java:12268)
    ... 1 more
Exception in thread "main" io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.RuntimeException: I am thrown
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
    at io.reactivex.Observable.subscribe(Observable.java:12275)
    at reactivex.ReactMain.main(ReactMain.java:36)
Caused by: java.lang.RuntimeException: I am thrown
    at reactivex.ReactMain$1.onNext(ReactMain.java:22)
    at reactivex.ReactMain$1.onNext(ReactMain.java:1)
    at io.reactivex.internal.operators.observable.ObservableFromArray$FromArrayDisposable.run(ObservableFromArray.java:108)
    at io.reactivex.internal.operators.observable.ObservableFromArray.subscribeActual(ObservableFromArray.java:37)
    at io.reactivex.Observable.subscribe(Observable.java:12268)
    ... 1 more
Exception in thread "main" java.lang.NullPointerException: Actually not, but can't throw other exceptions due to RS
    at io.reactivex.Observable.subscribe(Observable.java:12277)
    at reactivex.ReactMain.main(ReactMain.java:36)
Caused by: java.lang.RuntimeException: I am thrown
    at reactivex.ReactMain$1.onNext(ReactMain.java:22)
    at reactivex.ReactMain$1.onNext(ReactMain.java:1)
    at io.reactivex.internal.operators.observable.ObservableFromArray$FromArrayDisposable.run(ObservableFromArray.java:108)
    at io.reactivex.internal.operators.observable.ObservableFromArray.subscribeActual(ObservableFromArray.java:37)
    at io.reactivex.Observable.subscribe(Observable.java:12268)
    ... 1 more

我对此有两个疑问。

1)我已覆盖onError的{​​{1}}方法。为什么我的Observer无法捕获异常?

2)即使onError()失败了(我希望答案1中的原因),为什么UndeliverableException仅抛出两次?理想情况下,因为我还有其他4个onError字符串,它肯定已经抛出了4次?

1 个答案:

答案 0 :(得分:2)

1。

来自onError documentation

  

通知观察者可观察对象发生错误情况。

onError将不会被调用,因为可观察到的源没有错误。在 observer onNext方法中引发了错误。如果要测试onError,则需要在流中抛出错误,例如:

sourece
    .map( str -> throw new RuntimeException("I am thrown: " + str))
    .subscribe(myObserver);

以上代码将调用onError而不是onNext


2。 为什么UndeliverableException只抛出两次?

我认为UndeliverableException仅抛出一次,并且整个错误消息仅描述了一次崩溃。一旦您的代码在带有{alpha“的onNext方法处错误退出,此后就什么也不会发生。

尝试仅使用一个元素来运行代码,如下所示:

Observable<String> source = Observable.just("Alpha");

,看看是否收到相同的错误消息。另外,您可以检查是否发出任何东西:

Observable<String> source = Observable.just("Alpha", "Beta", "Gamma", "Upma", "Idly")
     .doOnNext(/* put log here to see what is being emitted */);