我正在学习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
的Observer
方法。为什么我的onError()
无法捕捉异常?
2)即使onError
失败(我希望答案1中的原因),为什么UndeliverableException只被抛出两次?理想情况下,自从我有4个其他Observable
字符串以来,它必须被抛出4次?
1.
来自onError
documentation:
通知Observer Observable遇到错误情况。
不会调用onError
,因为源可观察量中没有错误。错误是在观察者的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 */);