Exception handling in rxjava

大憨熊 提交于 2019-12-06 00:48:34

Use fromCallable that allows your method to throw (plus, it gets evaluated lazily and not before you even get into the Observable world):

rx.Observable.fromCallable(() -> new QuoteReader().getQuote())
          .subscribe(System.out::println, Throwable::printStackTrace);

There is another factory method for an Observable, which is called create. This gives you an Observer as input of a lambda expression. Observer is a kind of callback object with three methods: onNext(T t), onError(Throwable e) and onCompleted. I read you're new to RxJava, so here is a little extra as a sidenote: the Rx contract specifies that an Observer can receive zero or more onNext calls, followed by maybe either an onError or onCompleted call. In regular expression for this looks like: onNext* (onError|onCompleted)?.

Now that you know this, you can implement your operation using Observable.create:

Observable.create(observer -> {
  try {
    observer.onNext(new QuoteReader.getQuote());
    observer.onCompleted();
  }
  catch (Throwable e) {
    observer.onError(e);
  }
}

Notice that this code doesn't do anything until you subscribe to this Observable as you already did in your question!

Observable pipeline does not allow you throw Exceptions. You must use runtimeExceptions. So changing your code it should looks like.

     try(Response response = okHttpClient.newCall(request).execute()) {
        responseMap = gson.fromJson(response.body().string(), Map.class);
        System.out.println("response map : "+responseMap);
   } catch(IOException ioe) {
     ioe.printStackTrace();
     new RuntimeException(ioe);

You can see a practical example here https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/errors/ObservableExceptions.java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!