Monday, August 9, 2021

Java 8: Lambda: Exception Handling

Lambda expressions are short and concise way of representing functional interface implementation. However while handling exceptions code might become verbose and no more readable. Though there are various ways of handling lambda excpetions, demonstrating 2 ways in this article.

Personally my preferred way is with Wrapper, as it keeps the actual lambda code still a concise.

Let's see this with example

       

package java8;
import java.util.*;

public class EmployeeDB {
	public static List<Employee> getEmployeeList(){
		List<Employee> empList = Arrays.asList(
				//int eId, String fName, String lName, double salary, List<Integer> phNumbers
				new Employee(5, "Sonu", "Panjabi", 1000, Arrays.asList(1234, 2345)),
				new Employee(7, "Ganu", "Marathi", 1500, Arrays.asList(3456, 4567)),
				new Employee(8, "Manu", "Bangali", 2000, Arrays.asList(5678, 6789)),
				new Employee(4, "Tonu", "Bihari", 3000, Arrays.asList(7890, 8901)),
				new Employee(1, "Kanu", "Asami", 3500, Arrays.asList(9012, 1230)),
				new Employee(6, "Ranu", "Kashmiri", 4000, Arrays.asList(9876, 8765)),
				new Employee(2, "Janu", "Kannadi", 4500, Arrays.asList(7654, 6543)),
				new Employee(3, "Monu", "Gujrathi", 5000, Arrays.asList(5432, 4321))
				);
		return empList;
	}
}

Below code snippet will result in an ArithmeticException

       

package java8.lambdaExceptions;

import java8.EmployeeDB;

public class LambdaExceptionDemo {
    public static void main(String[] args) {
		MyLambdaExceptionDemo myLambdaExceptionDemo = () -> EmployeeDB.getEmployeeList().get(0).getSalary()/0;
		myLambdaExceptionDemo.arithmeticExcetion();
	}

	@FunctionalInterface
	public interface MyLambdaExceptionDemo{
		double arithmeticExcetion(); 	
	}	
}

This can be handled in following ways

1. Handling Exceptions within lambda body

       


package java8.lambdaExceptions;

import java8.EmployeeDB;

public class LambdaExceptionDemo {
	public static void main(String[] args) {
		try {
			MyLambdaExceptionDemo myLambdaExceptionDemo = () -> EmployeeDB.getEmployeeList().get(0).getSalary()/0;
			myLambdaExceptionDemo.arithmeticExcetion();
		}catch (ArithmeticException e) {
			System.out.println("Can't divide by 0, provide correct divisor");
		}catch (Exception e){
			System.out.println("Some other excepption"+e.getMessage());
		}
	}

	@FunctionalInterface
	public interface MyLambdaExceptionDemo{
		double arithmeticExcetion(); 	
	}	
}

Code shows how to handle the exception for Lambda within body, however the code does NOT look concise as above, so let's see below example where exception is handled in a wrapper lambda


2. Handling Exceptions with wrapper lambda

       


package java8.lambdaExceptions;

import java8.EmployeeDB;

public class LambdaExceptionDemo {
	public static void main(String[] args) {
		MyLambdaExceptionDemo myLambdaExceptionDemo = wrapperLambda((i) ->EmployeeDB.getEmployeeList().get(0).getSalary()/i);
		myLambdaExceptionDemo.arithmeticExcetion(0);
	}

	@FunctionalInterface
	public interface MyLambdaExceptionDemo{
		double arithmeticExcetion(int i); 	
	}

	static MyLambdaExceptionDemo wrapperLambda(MyLambdaExceptionDemo myLambdaExceptionDemo) {
		 return (i) -> {
			 try {
				myLambdaExceptionDemo.arithmeticExcetion(i);
			} catch (ArithmeticException e) {
				System.out.println("Can't divide by 0, provide correct divisor");
			} catch (Exception e) {
				System.out.println("Some other excepption" + e.getMessage());
			}
			 return -1;
		};
	}	
}

Above code still keeps the lambda code as concise. We can write different wrappers for different implementations as per requirement.

No comments:

SpringBoot: Features: SpringApplication

Below are a few SpringBoot features corresponding to SpringApplication StartUp Logging ·          To add additional logging during startup...