我一直在玩CompletableFuture
并注意到一件奇怪的事情。
String url = "http://google.com";
CompletableFuture<String> contentsCF = readPageCF(url);
CompletableFuture<List<String>> linksCF = contentsCF.thenApply(_4_CompletableFutures::getLinks);
linksCF.thenAccept(list -> {
assertThat(list, not(empty()));
});
linksCF.get();
如果,在我的thenAccept
调用时,断言失败,异常不会传播。
然后我尝试了更丑陋的东西:
linksCF.thenAccept(list -> {
String a = null;
System.out.println(a.toString());
});
什么也没有发生,也没有异常被传播。我尝试使用类似的方法handle
以及与例外情况相关的其他内容CompletableFutures
,但失败了 - 没有人按预期传播异常。
当我调试的时候CompletableFuture
,它确实捕获了这样的异常:
final void internalComplete(T v, Throwable ex) {
if (result == null)
UNSAFE.compareAndSwapObject
(this, RESULT, null,
(ex == null) ? (v == null) ? NIL : v :
new AltResult((ex instanceof CompletionException) ? ex :
new CompletionException(ex)));
postComplete(); // help out even if not triggered
}
没有别的。
我使用的是 JDK 1.8.0_05 x64、Windows 7。
我在这里错过了什么吗?