1

I am very new to RxAndroid and am still trying to navigate my way out of the errors that I am making.

Observable.just(RandomComputeManager.getChartData(0,"abcd",new Date()))
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    System.out.println("RXANDROID"+ s.getFood());
                    Toast.makeText(getActivity(),"HELLO"+s.getFood(),Toast.LENGTH_LONG);
                });

I have a heavy computation method here that I am trying to run on RxJava's Schedulers.computation() thread.(I do not know if only calling it in Observable.just is the right way). The method is supposed to throw an exception if it does not have data.

Class RandomComputeManager{
public static getPieChartData(int a,String b,Date c) throws CustomException {
   if(haveData){
             //All Okay
               }
   else{
       throw new CustomException("No Data");
       }

}

The build is failing with error

error: unreported exception CustomException; must be caught or declared to be thrown

I have tried adding an observer to the subscribe method thinking that it has a onError method but neither is that solving this issue nor am I able to fetch my data then due to some ambiguity in the return value of the called method(Don't know if it should be an observable or just the object I need). Please suggest a way to handle this.

arb93
  • 43
  • 10

3 Answers3

2

The subscriber function can take another argument of throwable.

Please do like this

Observable.just(RandomComputeManager.getChartData(0,"abcd",new Date()))
            .subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(s -> {
                System.out.println("RXANDROID"+ s.getFood());
                Toast.makeText(getActivity(),"HELLO"+s.getFood(),Toast.LENGTH_LONG);
            }, throwable ->{

});

Gautam Kumar
  • 351
  • 4
  • 7
  • Still getting this error- error: unreported exception CustomException; must be caught or declared to be thrown – arb93 Jun 17 '18 at 14:54
2
Observable.fromCallable(RandomComputeManager.getChartData(0,"abcd",new Date()))
            .subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(s -> {
                System.out.println("RXANDROID"+ s.getFood());
                Toast.makeText(getActivity(),"HELLO"+s.getFood(),Toast.LENGTH_LONG);
            }, Throwable :: printStackTrace);

This did the trick. Thanks to Gautam Kumar and Raj Suvariya for your help. I found this detail from Exception handling in rxjava

arb93
  • 43
  • 10
1

just method accepts parameters that are resolved immediately, so you actually are about to run the computation at the very same line you create your Observable. Also, this is the reason that your exception is not caught, as the getChartData is not called within Observable on-subscribe function.

What you need here is to create the Observable passing a computation function, but you are trying to pass the computed result.

I am used to Kotlin, so sorry if I mess up Java lambdas here, but you should use fromCallable here, like so

Observable.fromCallable(
        () -> RandomComputeManager.getChartData(0, "abcd", new Date())))

fromCallable accepts a function that will execute once you subscribe and will emit the function result.

Also, for your purpose it's better to use a Single, as you will have only one item emitted.

Single.fromCallable(
        () -> RandomComputeManager.getChartData(0, "abcd", new Date())))

Also if your your CustomException is checked and you don't want to crash, you do have to add onError handling, like already suggested by others.

Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99