gpt4 book ai didi

java-8 - RxJava 2.x 错误打印奇怪的堆栈跟踪

转载 作者:行者123 更新时间:2023-12-05 05:12:08 25 4
gpt4 key购买 nike

我正在学习 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) 我已经覆盖了 ObserveronError 方法。为什么我的 onError() 无法捕获异常?

2) 即使 onError 失败(我期待答案 1 中的原因),为什么 UndeliverableException 只抛出两次?理想情况下,它必须被抛出 4 次,因为我有 4 个其他 Observable 字符串?

最佳答案

1.

来自 onError documentation :

Notifies the Observer that the Observable has experienced an error condition.

onError 将不会被调用,因为源可观察对象中没有错误。错误是在 observeronNext 方法中引发的。如果你想测试 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 */);

关于java-8 - RxJava 2.x 错误打印奇怪的堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54878583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com