Java 使用runAsync时播放框架处理错误

Java 使用runAsync时播放框架处理错误,java,playframework-2.5,Java,Playframework 2.5,我在尝试处理负责数据插入的rest服务上的服务器错误时遇到问题 public CompletableFuture<Result> insertSomething() throws IOException { JsonNode jsNode = request().body().asJson(); format json node to be used } return CompletableFuture.runAsync(() -> {

我在尝试处理负责数据插入的rest服务上的服务器错误时遇到问题

public CompletableFuture<Result> insertSomething() throws IOException {
    JsonNode jsNode = request().body().asJson();
    format json node to be used
    }
    return CompletableFuture.runAsync(() -> {
        try {
            service.insertSomething(something);
        } catch (ParseException e) {
            internalServerError();
        }
    })
            .thenApply(future -> created("data inserted"))
            .exceptionally(ex -> internalServerError());
}
public CompletableFuture insertSomething()引发IOException{
JsonNode jsNode=request().body().asJson();
设置要使用的json节点的格式
}
返回CompletableFuture.runAsync(()->{
试一试{
服务。插入某物(某物);
}捕获(解析异常){
internalServerError();
}
})
.thenApply(未来->已创建(“插入数据”))
。异常情况下(ex->internalServerError());
}
internalServerError从未抛出,它一直在说“插入数据”。即使我发送了一些引发ParseException的数据。在调试模式下,我看到它在catch中传递,但没有抛出任何内容


提前感谢

这是因为异常被捕获了。如果你想抛出一个错误,你不需要捕捉它。

当然不会抛出任何东西-你捕捉到了吗?只有在
runAsync
内部抛出一个不由代码处理的异常时,
异常才会执行。

我找到了答案,我只需在catch中实例化一个类型为Throwable的对象,如下所示:

public CompletableFuture<Result> insertSomething() throws IOException {
JsonNode jsNode = request().body().asJson();
format json node to be used
}
return CompletableFuture.runAsync(() -> {
    try {
        service.insertSomething(something);
    } catch (ParseException e) {
        new Throwable(e.getMessage());
    }
})
        .thenApply(future -> created("data inserted"))
        .exceptionally(ex -> internalServerError());
public CompletableFuture insertSomething()引发IOException{
JsonNode jsNode=request().body().asJson();
设置要使用的json节点的格式
}
返回CompletableFuture.runAsync(()->{
试一试{
服务。插入某物(某物);
}捕获(解析异常){
新的可丢弃文件(如getMessage());
}
})
.thenApply(未来->已创建(“插入数据”))
。异常情况下(ex->internalServerError());

}

实际上,您只是在压制
ParseException
。您是否打算在
catch
块中编写
throw
语句?如果是这样,您需要抛出一个
RuntimeException
,这里
CompletionException
是最合适的,例如
抛出新的CompletionException(e)