karthiks
karthiks

Reputation: 7299

Handling Checked Exception in RxJava

I'm beginning to grasp RxJava1 (Now let's not get into why not RxJava2).

I've code like below:

MoviesAPI.findMovieURLsByType("comedy")
.map(s -> {
                    try {
                        return potentialCheckedException(s); //Throws CheckedException
                    } catch (MyExceptionType ex) {
                        return Exceptions.propagate(ex);
                    }
})
.subscribe(new Subscriber<String>() {
                    public void onCompleted() {
                        System.out.println("------Completed!-------");
                    }

                    public void onError(final Throwable e) {
                        System.out.println(e.getMessage());
                    }

                    public void onNext(final String s) {
                        System.out.println(s);
                    }
});

I get compile error like below:

Error:(28, 17) java: no suitable method found for subscribe(<anonymous rx.Subscriber<java.lang.String>>)
    method rx.Observable.subscribe(rx.functions.Action1<? super java.io.Serializable>) is not applicable
      (argument mismatch; <anonymous rx.Subscriber<java.lang.String>> cannot be converted to rx.functions.Action1<? super java.io.Serializable>)
    method rx.Observable.subscribe(rx.Observer<? super java.io.Serializable>) is not applicable
      (argument mismatch; <anonymous rx.Subscriber<java.lang.String>> cannot be converted to rx.Observer<? super java.io.Serializable>)
    method rx.Observable.subscribe(rx.Subscriber<? super java.io.Serializable>) is not applicable
      (argument mismatch; <anonymous rx.Subscriber<java.lang.String>> cannot be converted to rx.Subscriber<? super java.io.Serializable>)
    method rx.Observable.<T>subscribe(rx.Subscriber<? super T>,rx.Observable<T>) is not applicable
      (cannot infer type-variable(s) T
        (actual and formal argument lists differ in length))

If I were to remove the try-catch block and replace potentialCheckedException(s) with say noPotentialCheckedException(s), then the compile-error disappears. Why? What is it that I'm missing? Isn't wrapping CheckedException using Exceptions.propagate(ex) enough?

Thanks in advance.

Upvotes: 0

Views: 926

Answers (1)

akarnokd
akarnokd

Reputation: 70007

Exceptions.propagate returns RuntimeException and potentialCheckedException returns String I assume. The lambda return type inference then can only find a common supertype of Serializable which is not compatible with an explicitly typed String subscriber. You are more likely want to throw Exceptions.propagate(ex).

Upvotes: 1

Related Questions