2017-01-12 65 views
1

我正在將rxjava 1升級到rxjava 2.在rxjava 1中有OnErrorThrowable,但在rxjava中找不到2.在下面的代碼中,我可以做什麼來替換OnErrorThrowable?在rxjava中找不到OnErrorThrowable 2

static Observable<String> sampleObservable() { 
    return Observable.defer(new Callable<ObservableSource<String>>() { 
     @Override 
     public ObservableSource<String> call() throws Exception { 
      try { 
       // Do some long running operation 
       Thread.sleep(TimeUnit.SECONDS.toMillis(5)); 
      } catch (InterruptedException e) { 
       throw OnErrorThrowable.from(e); 
      } 
      Log.d(TAG, "The sleep is over, now produce something"); 
      return Observable.just("one", "two", "three", "four", "five"); 
     } 
    }); 
} 

回答

2

在2.x中,你不必爲所有的功能類型聲明throws Exception包裝例外:

static Observable<String> sampleObservable() { 
return Observable.defer(new Callable<ObservableSource<String>>() { 
    @Override 
    public ObservableSource<String> call() throws Exception { 
     Thread.sleep(TimeUnit.SECONDS.toMillis(5)); 
     Log.d(TAG, "The sleep is over, now produce something"); 
     return Observable.just("one", "two", "three", "four", "five"); 
    } 
}); 

}

+0

嘿大衛,怎麼樣()像'onErrorReturn情況下',在那裏我們以前用'OnErrorThrowable.from()'重新拋出錯誤? – Saket

2

您可以返回Observable.error(e);而不是拋出您的catch塊。