<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ### 異常 與 `CompletableFuture` 在處理鏈中包裝對象的方式相同,它也會緩沖異常。這些在處理時調用者是無感的,但僅當你嘗試提取結果時才會被告知。為了說明它們是如何工作的,我們首先創建一個類,它在特定的條件下拋出一個異常: ```java // concurrent/Breakable.java import java.util.concurrent.*; public class Breakable { String id; private int failcount; public Breakable(String id, int failcount) { this.id = id; this.failcount = failcount; } @Override public String toString() { return "Breakable_" + id + " [" + failcount + "]"; } public static Breakable work(Breakable b) { if (--b.failcount == 0) { System.out.println( "Throwing Exception for " + b.id + "" ); throw new RuntimeException( "Breakable_" + b.id + " failed" ); } System.out.println(b); return b; } } ``` 當`failcount` > 0,且每次將對象傳遞給 `work()` 方法時, `failcount - 1` 。當`failcount - 1 = 0` 時,`work()` 將拋出一個異常。如果傳給 `work()` 的 `failcount = 0` ,`work()` 永遠不會拋出異常。 注意,異常信息此示例中被拋出( `RuntimeException` ) 在下面示例 `test()` 方法中,`work()` 多次應用于 `Breakable`,因此如果 `failcount` 在范圍內,就會拋出異常。然而,在測試`A`到`E`中,你可以從輸出中看到拋出了異常,但它們從未出現: ```java // concurrent/CompletableExceptions.java import java.util.concurrent.*; public class CompletableExceptions { static CompletableFuture<Breakable> test(String id, int failcount) { return CompletableFuture.completedFuture( new Breakable(id, failcount)) .thenApply(Breakable::work) .thenApply(Breakable::work) .thenApply(Breakable::work) .thenApply(Breakable::work); } public static void main(String[] args) { // Exceptions don't appear ... test("A", 1); test("B", 2); test("C", 3); test("D", 4); test("E", 5); // ... until you try to fetch the value: try { test("F", 2).get(); // or join() } catch (Exception e) { System.out.println(e.getMessage()); } // Test for exceptions: System.out.println( test("G", 2).isCompletedExceptionally() ); // Counts as "done": System.out.println(test("H", 2).isDone()); // Force an exception: CompletableFuture<Integer> cfi = new CompletableFuture<>(); System.out.println("done? " + cfi.isDone()); cfi.completeExceptionally( new RuntimeException("forced")); try { cfi.get(); } catch (Exception e) { System.out.println(e.getMessage()); } } } ``` 輸出結果: ``` Throwing Exception for A Breakable_B [1] Throwing Exception for B Breakable_C [2] Breakable_C [1] Throwing Exception for C Breakable_D [3] Breakable_D [2] Breakable_D [1] Throwing Exception for D Breakable_E [4] Breakable_E [3] Breakable_E [2] Breakable_E [1] Breakable_F [1] Throwing Exception for F java.lang.RuntimeException: Breakable_F failed Breakable_G [1] Throwing Exception for G true Breakable_H [1] Throwing Exception for H true done? false java.lang.RuntimeException: forced ``` 測試 `A` 到 `E` 運行到拋出異常,然后…并沒有將拋出的異常暴露給調用方。只有在測試F中調用 `get()` 時,我們才會看到拋出的異常。 測試 `G` 表明,你可以首先檢查在處理期間是否拋出異常,而不拋出該異常。然而,test `H` 告訴我們,不管異常是否成功,它仍然被視為已“完成”。 代碼的最后一部分展示了如何將異常插入到 `CompletableFuture` 中,而不管是否存在任何失敗。 在連接或獲取結果時,我們使用 `CompletableFuture` 提供的更復雜的機制來自動響應異常,而不是使用粗糙的 `try-catch`。 你可以使用與我們看到的所有 `CompletableFuture` 相同的表單來完成此操作:在鏈中插入一個 `CompletableFuture` 調用。有三個選項 `exceptionally()`,`handle()`, `whenComplete()`: ```java // concurrent/CatchCompletableExceptions.java import java.util.concurrent.*; public class CatchCompletableExceptions { static void handleException(int failcount) { // Call the Function only if there's an // exception, must produce same type as came in: CompletableExceptions .test("exceptionally", failcount) .exceptionally((ex) -> { // Function if (ex == null) System.out.println("I don't get it yet"); return new Breakable(ex.getMessage(), 0); }) .thenAccept(str -> System.out.println("result: " + str)); // Create a new result (recover): CompletableExceptions .test("handle", failcount) .handle((result, fail) -> { // BiFunction if (fail != null) return "Failure recovery object"; else return result + " is good"; }) .thenAccept(str -> System.out.println("result: " + str)); // Do something but pass the same result through: CompletableExceptions .test("whenComplete", failcount) .whenComplete((result, fail) -> { // BiConsumer if (fail != null) System.out.println("It failed"); else System.out.println(result + " OK"); }) .thenAccept(r -> System.out.println("result: " + r)); } public static void main(String[] args) { System.out.println("**** Failure Mode ****"); handleException(2); System.out.println("**** Success Mode ****"); handleException(0); } } ``` 輸出結果: ``` **** Failure Mode **** Breakable_exceptionally [1] Throwing Exception for exceptionally result: Breakable_java.lang.RuntimeException: Breakable_exceptionally failed [0] Breakable_handle [1] Throwing Exception for handle result: Failure recovery object Breakable_whenComplete [1] Throwing Exception for whenComplete It failed **** Success Mode **** Breakable_exceptionally [-1] Breakable_exceptionally [-2] Breakable_exceptionally [-3] Breakable_exceptionally [-4] result: Breakable_exceptionally [-4] Breakable_handle [-1] Breakable_handle [-2] Breakable_handle [-3] Breakable_handle [-4] result: Breakable_handle [-4] is good Breakable_whenComplete [-1] Breakable_whenComplete [-2] Breakable_whenComplete [-3] Breakable_whenComplete [-4] Breakable_whenComplete [-4] OK result: Breakable_whenComplete [-4] ``` - `exceptionally()` 參數僅在出現異常時才運行。`exceptionally()` 局限性在于,該函數只能返回輸入類型相同的值。 - `exceptionally()` 通過將一個好的對象插入到流中來恢復到一個可行的狀態。 - `handle()` 一致被調用來查看是否發生異常(必須檢查fail是否為true)。 - 但是 `handle()` 可以生成任何新類型,所以它允許執行處理,而不是像使用 `exceptionally()`那樣簡單地恢復。 - `whenComplete()` 類似于handle(),同樣必須測試它是否失敗,但是參數是一個消費者,并且不修改傳遞給它的結果對象。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看