1
但是,testCase2不處理異常並引發錯誤。我錯過了什麼嗎?對不起,如果我這樣做,這是相當新的。期望從這兩個CompletableFutures獲得相同結果
@Test
public void testCase1() throws Exception {
CompletableFuture.supplyAsync(() -> {
if (true) throw new RuntimeException();
return "Promise";
}).exceptionally((ex) -> {
return "Fake Promise";
}).get();
}
@Test
public void testCase2() throws Exception {
CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> {
if (true) throw new RuntimeException();
return "Promise";
});
cf.exceptionally((ex) -> {
return "Fake Promise";
});
cf.get();
}
謝謝。我感到很愚蠢。 :/ – slee
沒有什麼大不了的,只是每個人在學習時都會犯的一些常見錯誤:-) – shizhz