如果已经处理了观察者,则RxJava2 Single内部抛出的RuntimeException不会被捕获,也不会被发送到Observer的onError()方法。
我理解调用CompositeDisposable.dispose()意味着我的订阅者的onSuccess或onError方法不会被调用,但是也不应该忽略该异常?而是应用程序崩溃,因为没有捕获到RuntimeException。
这是Single,带注释的行可以抛出RuntimeException,如果在调用.dispose()方法之前抛出异常,它会被捕获并且错误被发送给订阅者,但是如果异常在调用.dispose()后,应用程序崩溃了。
public Single<Submission> getSubmission(final String threadId, final CommentSort sort) {
return Single.create(new SingleOnSubscribe<Submission>() {
@Override
public void subscribe(SingleEmitter<Submission> e) throws Exception {
// some irrelevant code
try {
submission = reddit.getSubmission(sr); // Can throw RuntimeException
e.onSuccess(submission);
} catch (Exception ex) {
e.onError(ex);
}
}
});
}
我订阅的代码:
disposables.add(someCompletable
.andThen(service.getSubmission(threadId, CommentSort.HOT))
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.subscribeWith(new DisposableSingleObserver<Submission>() {
@Override
public void onSuccess(Submission submission) {
// Handle result
}
@Override
public void onError(Throwable e) {
// Handle error
}
})
);
Stacktrace:
E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-2
Process: com.gmail.jorgegilcavazos.ballislife.debug, PID: 15242
java.lang.RuntimeException: Unable to parse JSON: [{"kind": "Listing", "data": {"modhash": null, "children": [{"kind": "t3", "data
at net.dean.jraw.util.JrawUtils.fromString(JrawUtils.java:182)
at net.dean.jraw.http.RestResponse.<init>(RestResponse.java:64)
at net.dean.jraw.http.OkHttpAdapter.execute(OkHttpAdapter.java:81)
at net.dean.jraw.http.RestClient.execute(RestClient.java:120)
at net.dean.jraw.RedditClient.execute(RedditClient.java:143)
at net.dean.jraw.RedditClient.execute(RedditClient.java:137)
at net.dean.jraw.RedditClient.getSubmission(RedditClient.java:287)
at com.gmail.jorgegilcavazos.ballislife.network.API.RedditService$10.subscribe(RedditService.java:311)
at io.reactivex.internal.operators.single.SingleCreate.subscribeActual(SingleCreate.java:39)
at io.reactivex.Single.subscribe(Single.java:2656)
at io.reactivex.internal.operators.single.SingleDelayWithCompletable$OtherObserver.onComplete(SingleDelayWithCompletable.java:70)
at io.reactivex.internal.disposables.EmptyDisposable.complete(EmptyDisposable.java:67)
at io.reactivex.internal.operators.completable.CompletableEmpty.subscribeActual(CompletableEmpty.java:27)
at io.reactivex.Completable.subscribe(Completable.java:1592)
at io.reactivex.internal.operators.single.SingleDelayWithCompletable.subscribeActual(SingleDelayWithCompletable.java:36)
at io.reactivex.Single.subscribe(Single.java:2656)
答案 0 :(得分:2)
找到答案:
RxJava 2的设计使得不会丢失任何错误。因此,当序列结束或被取消时,发射器无处发送错误,因此它被路由到RxJavaPlugins.onError
。
与1.x不同,2.x默认情况下调用Thread.currentThread().getUncaughtExceptionHandler().uncaughtException()
会导致Android应用崩溃。
Source
Similar stackoverflow question
在我的情况下,由于订户已被处理,我不再关心发射器可能抛出的错误,因此发射器应在调用onError()
try {
Submission submission = redditClient.getSubmission(sr);
e.onSuccess(submission);
} catch (Exception ex) {
if (!e.isDisposed()) {
e.onError(ex);
}
}