vararo27 commented on PR #1918: URL: https://github.com/apache/cassandra-java-driver/pull/1918#issuecomment-1993839304
Actually, exception thrown by below line will not move to the whenComplete block following that. Basically, problem is that we have logic and exception thrown outside of thenApplyAsync. ` schemaQueriesFactory .newInstance() .execute() ` Attaching one sample java program which could be tried out to highlight the same problem. It will block the thread and not print the final result because of exception thrown in > FetchData.newInstance().fetchData() `package org.example.java8; import com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures; import java.util.concurrent.CompletableFuture; public class CompletableFutureMain { public static void main(String[] args) { CompletableFuture<Integer> resultFuture = new CompletableFuture<>(); CompletableFuture<String> fetchFuture = CompletableFuture.supplyAsync(CompletableFutureMain::fetchData); CompletableFuture<Integer> processFuture = fetchFuture.thenCompose(CompletableFutureMain::processAsync); processFuture.whenComplete((result, exception) -> { if (exception != null) { resultFuture.completeExceptionally(exception); System.out.println("Exception occurred : " + exception.getMessage()); } else { System.out.println("Processed result : " + result); /* try {*/ FetchData.newInstance().fetchData()/*.exceptionally(ex -> { resultFuture.completeExceptionally(ex); return "9"; })*/ .thenApplyAsync(CompletableFutureMain::processData) .whenComplete((result1, exception1) -> { if (exception1 != null) { resultFuture.completeExceptionally(exception1); System.out.println("Exception occurred 1 : " + exception1.getMessage()); } else { resultFuture.complete(result1); System.out.println("Processed result 1 : " + result1); } }); /*} catch (Exception exception2) { resultFuture.completeExceptionally(exception2); System.out.println("No result"); }*/ } }); Integer result = CompletableFutures.getUninterruptibly(resultFuture); System.out.println("Final Result " + result); } // Simulated data processing operation private static Integer processData(String data) { int value = Integer.parseInt(data); /*throw new IllegalStateException("Error while processing data !!");*/ return value * 3; } // Simulated data fetching operation private static String fetchData() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "10"; } // Simulated data processing operation private static CompletableFuture<Integer> processAsync(String data) { return CompletableFuture.supplyAsync(() -> { int value = Integer.parseInt(data); return value * 2; }); } } class FetchData { private final CompletableFuture<String> resultFuture = new CompletableFuture<>(); private FetchData() { } public static FetchData newInstance() { return new FetchData(); } // Simulated data fetching operation public CompletableFuture<String> fetchData() { try { Thread.sleep(1000); throw new IllegalStateException("Error in fetching data !!"); } catch (InterruptedException e) { e.printStackTrace(); } resultFuture.complete("10"); return resultFuture; } }` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org For additional commands, e-mail: commits-h...@cassandra.apache.org