In this post we’ll see exception handling with lambda expressions in Java. A lambda expression can throw an exception but that should be compatible with the exceptions specified in the throws clauses of the abstract method of the functional interface.
If a lambda expression body throws a checked exception, the throws clause of the functional interface method must declare the same exception type or its supertype.
Checked exception in lambda expression
In the example abstract method calculate of the functional interface MyFunctionalInterface doesn't have a throws clause but lambda body throws an exception. In such case a compiler error message "Unhandled exception type Exception" is generated.
If you specify the same exception in the functional interface method then the error is resolved but then you do need to handle it while calling the method.
@FunctionalInterface interface MyFunctionalInterface{ int calculate(int i, int j) throws Exception; } public class LambdaException { public static void main(String[] args) { MyFunctionalInterface ref = (x, y) -> { int num = x * y; throw new Exception(); //return num; }; try { System.out.println("Result is " + ref.calculate(8, 5)); }catch(Exception e) { System.out.println("Exception while calculating " + e.getMessage()); } } }
Unchecked exception in lambda expression
In case of unchecked exception there is no such restriction that it should be specified in the abstract method of the functional interface.
In the example abstract method calculate of the functional interface MyFunctionalInterface doesn't have a throws clause but lambda body throws a runtime exception.
@FunctionalInterface interface MyFunctionalInterface{ int calculate(int i, int j); } public class LambdaException { public static void main(String[] args){ MyFunctionalInterface ref = (x, y) -> { try { int num = x/y; return num; }catch(ArithmeticException e) { System.out.println("Exception while calculating- " + e.getMessage()); throw new RuntimeException(e); } }; System.out.println("Result is " + ref.calculate(8, 0)); } }
Wrapping lambda to handle exceptions
Many people like to keep lambda expression code concise and clutter free with no try catch blocks.
In that case you can create a wrapper class wrapping your lambda and then calling it.
@FunctionalInterface interface MyFunctionalInterface{ int calculate(int i, int j); } public class LambdaException { // Lambda wrapper static MyFunctionalInterface lambdaWrapper(MyFunctionalInterface ref) { return (x, y) -> { try { ref.calculate(x, y); }catch(ArithmeticException e) { System.out.println("Exception while calculating- " + e.getMessage()); } return -1; }; } public static void main(String[] args){ // calling lambda wrapp MyFunctionalInterface ref = lambdaWrapper((x,y)->x/y); System.out.println("Result is " + ref.calculate(8, 0)); } }Output
Exception while calculating- / by zero Result is -1Reference: https://www.oracle.com/technetwork/articles/java/lambda-1984522.html
That's all for the topic Exception Handling With Java Lambda Expressions. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Java Lambda Expressions With Examples
- Method Reference in Java
- BinaryOperator Functional Interface Java Examples
- Java Stream distinct() With Examples
- Java Executor Tutorial - Executor, ExecutorService, ScheduledExecutorService
- How to Convert float to String in Java
- Constructor Dependency Injection in Spring
- React App Flow - create-react-app Structure
No comments:
Post a Comment