是否可以使用ComplatableFuture在链中制作出色的处理程序? 例如:我有这个代码,想法是如果位置服务方法抛出错误,则对findClosest方法进行泛型调用(没有参数)。基本上,我想返回List< Closest>形成这些方法之一。这没问题。但是在外部代码上,我需要触发一个事件,以防方法调用是通用的(如果locationService失败)。
library(dplyr)
df_av <- df %>% group_by(bleach_time) %>% summarise_all(mean)
在测试中,异常部分从未执行过,导致未来似乎成功完成。
@Async
@Override
public CompletionStage<List<Closest>> getClosestByZip(final String zip) {
return locationService.getCoordinates(zip)
.handle((c, ex) -> ex == null ? closestService.findClosest(c) : closestService.findClosest())
.thenCompose(list -> list);
}
如何处理这种情况?
答案 0 :(得分:-2)
请检查这是否有用。
public class CompleteableFutureEx {
public static void main(String[] args) throws Throwable {
try {
test(-1);
} catch (ArithmeticException e) {
System.out.println("Oops! We have an ArithmeticException");
}
catch (IllegalArgumentException e) {
System.out.println("Oops! We have an IllegalArgumentException");
}
catch (Exception e) {
System.out.println("Oops! We have an Exception ");
}
}
public static void test(int age) throws Throwable {
try {
CompletableFuture<String> maturityFuture = CompletableFuture.supplyAsync(() -> {
//ArithmeticException
//int age1 = age/0;
if (age < 0) {
throw new IllegalArgumentException("Age can not be negative");
}
if (age > 18) {
return "Adult";
} else {
return "Child";
}
});
maturityFuture.join();
}catch (CompletionException ce) {
throw ce.getCause();
}
}
}