映射异常在未来的完整的不同的异常类型? [英] Map exception in completable future to a different exception type?

查看:123
本文介绍了映射异常在未来的完整的不同的异常类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java 8的完整期货,我希望能够在将来抛出一个例外,并将其转换为不同的异常。



一旦出现异常,我尝试的所有复合材料似乎都会短路。



使用例如,scala的未来,我可以这样做:

  scala.concurrent.Future< Object> translateException = ask.recover(new Recover< Object>(){
@Override public Object recover(final Throwable failure)throws Throwable {
if(failure instanceof AskTimeoutException){
throw new ApiException失败);
}

throw failure;
}
},actorSystem.dispatcher());

我希望能够在未来的Java复合模块中模仿。这可能吗?你可以使用 CompletableFuture#handle(BiFunction) 。例如

  CompletableFuture< String> ask = CompletableFuture.supplyAsync(() - > {
throw new IndexOutOfBoundsException();
});
CompletableFuture< String> translateException = ask.handle((r,e) - > {
if(e!= null){
if(e instanceof IndexOutOfBoundsException){
throw new IllegalArgumentException();
}
throw(RuntimeException)e; //这是粗略的,处理它不同,也许通过将它包装在一个RuntimeException
}
return r;
});

如果要求完成异常,那么 translatedException 将完成一个可能转换的异常。



关于我在代码中的评论, handle 方法期望 BiFunction 应用方法未声明抛出 Throwable 。因此,lambda主体本身不能抛出 Throwable 。参数 e 的类型为 Throwable ,所以你不能 throw 它直接。如果你知道它是那种类型的,你可以将它转换为 RuntimeException ,或者你可以将它包装在一个 RuntimeException throw that。


I'm using java 8's completable futures and I'd like to be able take an exception that is throwing by the future and transform it to a different exception.

All the composite stuff I've tried seems to get short circuited once an exception occurs.

Using a scala future, for example, I can do something like this:

scala.concurrent.Future<Object> translatedException = ask.recover(new Recover<Object>() {
            @Override public Object recover(final Throwable failure) throws Throwable {
                if (failure instanceof AskTimeoutException) {
                    throw new ApiException(failure);
                }

                throw failure;
            }
        }, actorSystem.dispatcher());

and I'd like to be able to mimic that in a future composite block in java. Is this possible?

解决方案

You can use CompletableFuture#handle(BiFunction). For example

CompletableFuture<String> ask = CompletableFuture.supplyAsync(() -> {
    throw new IndexOutOfBoundsException();
});
CompletableFuture<String> translatedException = ask.handle((r, e) -> {
    if (e != null) {
        if (e instanceof IndexOutOfBoundsException) {
            throw new IllegalArgumentException();
        }
        throw (RuntimeException) e; // this is sketchy, handle it differently, maybe by wrapping it in a RuntimeException
    }
    return r;
});

If ask completed with an exception, then translatedException will complete with a potentially transformed exception. Otherwise, it will have the same success result value.

Concerning my comment in the code, the handle method expects a BiFunction whose apply method is not declared to throw a Throwable. As such, the lambda body cannot itself throw a Throwable. The parameter e is of type Throwable so you can't throw it directly. You can cast it to RuntimeException if you know it's of that type, or you can wrap it in a RuntimeException and throw that.

这篇关于映射异常在未来的完整的不同的异常类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆