'Java CompleteableFuture .handle() access input parameter from previous chained method

Is it possible to access the input to a previous chained .thenCompose() from within the .handle()?

In the following block, I want to reduce the accumulated x if there is an exception.

@Test
public void Test() {
    Integer result = testCompletable().join();
    assertThat(result.equals(1));
}
public CompletableFuture<Integer> testCompletable() {
    CompletableFuture<Integer> result = CompletableFuture.completedFuture(0);
    for (int i = 0; i < 2; i++) {
        final int j = i;
        result = result.thenCompose(x -> {
            if (j == 1) {
              throw new RuntimeException("Manually planted ex");
            }
            x = x + 1;
            return CompletableFuture.completedFuture(x);
        }).handle((r, e) -> {
            if (e != null) {
                x = x -1;   // <-- this line doesn't compile, how to access x here?
            }
            return x;
        });
    }
    return result;
}

I originally had

.handle((r, e) -> {
                if (e != null) {
                    r = r -1;   // r is NULL here.
                }
                return r;
            })


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source