如何在FlowableOnSubscribe中抛出异常?

时间:2017-06-15 14:29:47

标签: android observable rx-java2

Flowable.create(new FlowableOnSubscribe<Object>() {
   @Override
   public void subscribe(@io.reactivex.annotations.NonNull FlowableEmitter<Object> e) throws Exception {
     //throws exceptions, how can I handle error  it using RxJava
   }},BackpressureStrategy.LATEST);

取决于情况,它可能会导致问题,如何处理,我不想使用if条件。

1 个答案:

答案 0 :(得分:0)

您需要在订阅者中定义onError:

.subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String o) throws Exception {
                        Log.d("test", o);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        Log.e("test", "throwable: " + throwable.getMessage());
                    }
                });

然后订阅者将处理throwable。