How to use Observable.fromCallable() with a checked Exception?

前端 未结 2 874
面向向阳花
面向向阳花 2021-02-08 06:07

Observable.fromCallable() is great for converting a single function into an Observable. But how do you handle checked exceptions that might be thrown by the functio

2条回答
  •  别跟我提以往
    2021-02-08 06:55

    Rather than using a Func0 with Observable.fromCallable(), use Callable. For example:

    Observable.fromCallable(new Callable() {
        @Override
        public File call() throws Exception {
            return downloadFileFromNetwork();
        }
    }
    

    Since Callable's method call() throws Exception, you don't have to wrap your function in a try-catch! This must be what the lambda is using under the hood.

提交回复
热议问题