Java 8 CompletableFuture Exception Handling | handle() with NPException ArithmeticException

 Hi All,

Today I have released new video on "Java 8 CompletableFuture Exception Handling | handle() with NPException ArithmeticException"

This is frequently asked question in Java Interview.

Please watch full video, share, like and Subscribe Youtube channel and press bell icon. So you will get latest video notification.

 

CompletableFuture Exception Handling | handle() with NPException ArithmeticException




 package org.practice.basicprgms;


import java.util.concurrent.CompletableFuture;

import java.util.concurrent.ExecutionException;


public class ExceptionHandlingInCompletableFuture {


public static void main(String[] args) throws InterruptedException, ExecutionException {

// 1. Handle ArithmeticException Using exceptionally()

CompletableFuture<Integer> future1 =

CompletableFuture.supplyAsync(() -> {

int totalSalary = 50000;

int empCount = 10;

return totalSalary / empCount;

}).exceptionally(ex -> {

System.out.println("Exception : "+ ex.getMessage());

return -1;

});

System.out.println("Average Salary : "+ future1.get());

// 2. Handle NullPointerException Using exceptionally()

CompletableFuture<Integer> future2 =

CompletableFuture.supplyAsync(() -> {

String name = "Yogesh";

return name.length();

}).exceptionally(ex -> {

System.out.println("Exception : "+ ex.getMessage());

return -1;

});

System.out.println("Length : "+ future2.get());

// 3. Handle Multiple Exception Using handle() method

CompletableFuture<Integer> future3 =

CompletableFuture.supplyAsync(() -> {

String name = "Ronny";

if(name == null) {

throw new NullPointerException();

}

int salary = 50000;

int empCount = 10;

return salary / empCount;

}).handle((result, ex) -> {

if(ex != null) {

Throwable t = ex.getCause();

if(t instanceof NullPointerException)

return -1;

else if(t instanceof ArithmeticException)

return -2;

}

return result;

});

System.out.println("Average Salary :"+ future3.get());


// 4. Logged Exception(ArithmeticException) Using whenComplete()

CompletableFuture<Integer> future4 =

CompletableFuture.supplyAsync(() -> 100/10)

.whenComplete((result, ex) -> {

if(ex != null)

System.out.println("Exception : "+ ex.getMessage());

});

System.out.println("Result future 4 :"+ future4.get());


// 5. Manually Failed the Future Using completeExceptionally()

CompletableFuture<Integer> future5 = new CompletableFuture<Integer>();

future5.completeExceptionally(new RuntimeException("Database Down"));

System.out.println("Exception : "+ future5.get());

}

}


Output:

Exception : java.lang.ArithmeticException: / by zero

Average Salary : -1

Exception : java.lang.NullPointerException: Cannot invoke "String.length()" because "name" is null

Length : -1

Average Salary :-1

Exception : java.lang.ArithmeticException: / by zero

Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero

at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396)

at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2073)

at org.practice.basicprgms.ExceptionHandlingInCompletableFuture.main(ExceptionHandlingInCompletableFuture.java:63)

Caused by: java.lang.ArithmeticException: / by zero

at org.practice.basicprgms.ExceptionHandlingInCompletableFuture.lambda$6(ExceptionHandlingInCompletableFuture.java:58)

at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)

at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1760)

at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)

at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)

at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)

at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)

at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)

Post a Comment

0 Comments