我在CompletableFuture
中发生异常时尝试设置默认值。我按handle
方法使其工作如下:
private static void testHandle() {
String name = null;
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(() -> {
if (name == null) {
throw new RuntimeException("Computation error!");
}
return "Hello, " + name;
}).handle((s, t) -> s != null ? s : "Hello, Stranger!" + t.toString());
out.println(completableFuture.join());
}
但当我尝试在发生不良事件时使用CompletableFuture
停止completeExceptionally
并跟踪异常,如下所示我无法抓住异常就像我刚才那样。
private static void testCompleteExceptionally() {
String name = "Hearen";
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(() -> {
delay(500L);
if (name == null) {
throw new RuntimeException("Computation error!");
}
return "Hello, " + name;
}).handle((s, t) -> {
try {
throw t.getCause();
} catch (Throwable e) {
out.println(e.toString()); // I was hoping to record the custom exceptions here;
}
return s != null ? s : "Hello, Stranger!" + t.toString();
});
if (name != null) {
completableFuture.completeExceptionally(new RuntimeException("Calculation failed!")); // when bad things happen, I try to complete it by exception;
}
out.println(completableFuture.join());
}
更新 2018-06-09感谢您的帮助,@ Daniele
private static void testCompleteExceptionally() {
String name = "Hearen";
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(() -> {
delay(500L);
if (name == null) {
throw new RuntimeException("Computation error!");
}
return "Hello, " + name;
});
if (name != null) {
completableFuture.completeExceptionally(new RuntimeException("Calculation failed!"));
}
out.println(completableFuture.handle((s, t) -> s != null ? s : "Hello, Stranger!" + t.toString()).join());
}
join()
之前的句柄按预期工作。但在这种情况下,returned value
将为null
。
返回 new CompletionStage,当此阶段正常或异常完成时,将使用此阶段的结果和异常作为所提供函数的参数执行。
答案 0 :(得分:0)
您正在构建一个future
,并使用handle
(以便获得另一个未来),然后完成handle
返回的特殊未来。
你应该特别完成内部future
本身,而不是handle
。
这里的要点是handle
返回另一个未来;并且你不应该特别完成“外部”未来,因为这样做会绕过处理行为。
代码下方;
package stackOv;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Supplier;
public class TestHandle {
BiFunction<String, Throwable, String> handle2 = new BiFunction<String, Throwable, String>() {
@Override
public String apply(String s, Throwable t) {
try {
throw t.getCause();
} catch (Throwable e) {
// I was hoping to record the custom exceptions here;
System.out.println(e.toString());
}
return s != null ? s : "Hello, Stranger!" + t.toString();
}
};
private void testCompleteExceptionally() {
String name = "Hearen";
Supplier<String> supplier2 = () -> {
delay(500L);
if (name == null) {
throw new RuntimeException("Computation error!");
}
return "Hello, " + name;
};
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(supplier2);
if (name != null) {
// when bad things happen, I try to complete it by exception;
completableFuture.completeExceptionally(new RuntimeException("Calculation failed!"));
}
System.out.println(completableFuture.handle(handle2).join());
}
public static void main(String[] args) {
TestHandle th = new TestHandle();
th.testCompleteExceptionally();
}
private static void delay(long milli) {
try { Thread.sleep(milli); } catch (InterruptedException e) {}
}
}