简体   繁体   English

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

[英]Error on RxJava 2.x prints weird stacktrace

I am learning RxJava(Absolutely new, Sorry if this question is too basic), and I am having difficulty on error handling mechanism(I have gone through docs, but that is way advanced for me to grasp). 我正在学习RxJava(绝对是新问题,如果这个问题太基础了,抱歉),并且我在错误处理机制上遇到了困难(我已经阅读过文档,但这对我来说很高级)。

This is my code, 这是我的代码

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);
  }

And this is my stacktrace 这是我的堆栈跟踪

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

I have two questions around this. 我对此有两个问题。

1) I have overridden onError method of Observer . 1)我已经重写了Observer onError方法。 Why is my onError() not able to catch the exceptions? 为什么我的onError()无法捕获异常?

2) Even if the onError fails(I expect a why in answer 1), Why is UndeliverableException is thrown only twice? 2)即使onError失败(我希望答案1中的原因),为什么UndeliverableException只抛出两次? Ideally, it must have been thrown 4 times since I have 4 other Observable Strings? 理想情况下,因为我还有其他4个Observable字符串,所以它必须抛出4次?

1. 1。

From onError documentation : onError 文档

Notifies the Observer that the Observable has experienced an error condition. 通知观察者可观察对象发生错误情况。

onError will not be called because there was no error in the source observable. onError将不会被调用,因为可观察到的源中没有错误。 The error was thrown in onNext method of the observer . 错误是由观察者的 onNext方法引发的。 If you want to test onError you need to throw error inside the stream, for example: 如果要测试onError ,则需要在流中抛出错误,例如:

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

Above code will call onError instead of onNext . 上面的代码将调用onError而不是onNext


2. Why is UndeliverableException is thrown only twice? 2. 为什么UndeliverableException只抛出两次?

I think UndeliverableException is thrown only once, and the whole error message describes just that one crash. 我认为UndeliverableException抛出一次,并且整个错误消息仅描述了一次崩溃。 As soon as your code exit with error at onNext method with "alpha", nothing should happen after that. 一旦您的代码在onNext方法中使用“ alpha”错误退出时, onNext不会发生任何事情。

Try running your code with just one element, like this: 尝试仅使用一个元素来运行代码,如下所示:

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

and see if you get the same error message. 并查看是否收到相同的错误消息。 Also, you can check if anything is emitted: 另外,您可以检查是否发出任何东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM