2017-07-07 15 views
0

私はRxJava1を理解し始めています(今はRxJava2にならないようにしましょう)。RxJavaのチェック例外の処理

私は以下のようなコードをしました:

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); 
        } 
}); 

私は以下のようなコンパイルエラーを取得:

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)) 

私は、その後、try-catchブロックを削除し、noPotentialCheckedException(s)言うとpotentialCheckedException(s)を交換した場合コンパイルエラーが消えます。どうして?私は何が欠けているのですか? Exceptions.propagate(ex)を使用してCheckedExceptionをラップしていませんか?

ありがとうございます。

答えて

1

Exceptions.propagate返信RuntimeExceptionpotentialCheckedExceptionStringとします。ラムダ戻り型の推論は、Serializableという共通のスーパータイプしか見つけることができず、明示的に型指定されたStringサブスクライバと互換性がありません。 throw Exceptions.propagate(ex)にお勧めします。

+0

愚かな私。ありがとう@akarnokd。私はRxJavaで何が欠けているのか疑問に漂っていました.RxJavaは実際にJavaの基本にまで沸き立っています。 – karthiks