与 CompletableFuture 中的异常处理等效的 finally 块

Nir*_*ane 5 java-8 completable-future

我有CompletableFuture哪个可以返回结果或异常。我想在出现异常和正常结果的情况下执行一些通用代码。类似于try catch finally

当前实施

CompletableFuture<Integer> future= CompletableFuture.supplyAsync(this::findAccountNumber)
                             .thenApply(this::calculateBalance)                      
                             .thenApply(this::notifyBalance)
                             .exceptionally(ex -> {
                                 //My Exception Handling logic 
                                 return 0;
                             });
Run Code Online (Sandbox Code Playgroud)

我可以把我的最终逻辑放在哪里?

Hol*_*ger 6

最接近的等价物finallywhenComplete。就像handle,它接受一个接收结果值或 throwable 的函数,但它不提供替换结果值,相反,新的完成阶段不会改变结果,就像finally

所以

static int decode(String s) {
    try {
        return Integer.parseInt(s);
    }
    finally {
        System.out.println("finally action");
    }
}
Run Code Online (Sandbox Code Playgroud)

相当于

static int decode1(String s) {
    return CompletableFuture.completedFuture(s)
        .thenApply(Integer::parseInt)
        .whenComplete((i,t) -> System.out.println("finally action"))
        .join();
}
Run Code Online (Sandbox Code Playgroud)

所以当与

for(String s: Arrays.asList("1234", "foo bar")) try {
    System.out.println("decoded: "+decode(s));
} catch(Exception ex) {
    System.out.println("decoding "+s+" failed with "+ex);
}
Run Code Online (Sandbox Code Playgroud)

第一个变体印刷品

static int decode(String s) {
    try {
        return Integer.parseInt(s);
    }
    finally {
        System.out.println("finally action");
    }
}
Run Code Online (Sandbox Code Playgroud)

后者打印

static int decode1(String s) {
    return CompletableFuture.completedFuture(s)
        .thenApply(Integer::parseInt)
        .whenComplete((i,t) -> System.out.println("finally action"))
        .join();
}
Run Code Online (Sandbox Code Playgroud)

两者的共同点是,如果 try 块/前一阶段异常完成,则在 finally 操作中抛出的异常将取代原始结果,掩盖异常。