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

Sre*_*ree 18 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...);
Run Code Online (Sandbox Code Playgroud)

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

wnc*_*_21 21

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);
        }
}
Run Code Online (Sandbox Code Playgroud)

  • 来吧.IOException不是运行时异常.再读一遍我的答案. (3认同)
  • 你必须抛出RuntimeException.JVM不会要求你用try catch包围它.这就是主意 (2认同)

aka*_*okd 7

使用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(...)
Run Code Online (Sandbox Code Playgroud)

编辑:

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