如何在RxJava中的Observable中处理map()中的异常

时间:2015-11-05 22:34:15

标签: java android rx-java rx-android

我想这样做:

Observable.just(bitmap)
            .map(new Func1<Bitmap, File>() {
                @Override
                public File call(Bitmap photoBitmap) {

                    //File creation throws IOException, 
                    //I just want it to hit the onError() inside subscribe()

                    File photoFile = new File(App.getAppContext().getCacheDir(), "userprofilepic_temp.jpg");
                    if(photoFile.isFile()) {//delete the file first if it exists otherwise the new file won't be created
                        photoFile.delete();
                    }
                    photoFile.createNewFile(); //saves the file in the cache dir

                    FileOutputStream fos = new FileOutputStream(photoFile);
                    photoBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);//jpeg format
                    fos.close();

                    return photoFile;

                }
            })
            .subscribe(//continue implementation...);

基本上在call()方法中,它可以抛出异常。如何让Observer在onError()中处理它。或者这不是思考这个问题的正确方法吗?

4 个答案:

答案 0 :(得分:18)

rx将始终捕获错误,即使这是RuntimeException。 所以你可以在catch块中抛出某种运行时异常。这实际上应该是如何工作的。

 Observable.just(bitmap)
                .map(b -> {
                    try {
                        // do some work which throws IOException
                        throw new IOException("something went wrong");
                    } catch (IOException e) {
                        throw new RXIOException(e);
                        // Or you can use 
                        throw Exceptions.propagate(e);
                        // This helper method will wrap your exception with runtime one
                    }
                }).subscribe(o -> {
                    // do something here
                }, exception -> exception.printStackTrace());

public static class RXIOException extends RuntimeException {
        public RXIOException(IOException throwable) {
            super(throwable);
        }
}

答案 1 :(得分:6)

使用1.0.15,有fromCallable工厂方法,让你为每个订阅者运行一个Callable实例,你也可以抛出已检查的异常:

Observable.fromCallable(() -> {      
    File photoFile = new File(App.getAppContext().getCacheDir(),
        "userprofilepic_temp.jpg");
    if (photoFile.isFile()) {
       //delete the file if it exists otherwise the new file won't be created
        photoFile.delete();
    }
    photoFile.createNewFile(); //saves the file in the cache dir

    FileOutputStream fos = new FileOutputStream(photoFile);
    photoBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);//jpeg format
    fos.close();

    return photoFile;
})
.subscribe(...)

编辑:

source.flatMap(v -> {
    try {
        //...
        return Observable.just(result);
    } catch (Exception e) {
        return Observable.error(e);
    }
})
.subscribe(...);

答案 2 :(得分:3)

刚刚创建了一个帮助类来将这个样板提取到另一个地方:

public class RxRethrow {
    public static <T, R> Func1<T, R> rethrow(Func1R<T, R> catchedFunc) {
        return t -> {
            try {
                return catchedFunc.call(t);
            } catch (Exception e) {
                throw Exceptions.propagate(e);
            }
        };
    }

    public interface Func1R<T, R> extends Function {
        R call(T t) throws Exception;
    }
}

您可以这样称呼它:

.map(RxRethrow.rethrow(products -> mapper.writer(schema).writeValueAsString(products)))

答案 3 :(得分:2)

我不知道在第一次提出并回答这个问题时情况如何,但RxJava目前包含一个帮助方法,用于此目的: BackgroundGraphic="{Binding MyService, Path=MyUri,Converter={StaticResource SomeConverter}, ConverterParameter="Image1"}" //Will not work

RxJava Javadoc

  

直接抛出RuntimeException和Error或将任何其他异常类型包装到RuntimeException中的便捷方法。