Exception handling using wrapper lamda

Below is the example where you can wrap a lambda inside a lambda.

import java.util.function.BiConsumer;

public class WrapperLambda {
	public static void main(String[] args) {
		int[] inputNumbers = { 1, 2, 3, 4, 5 };
		int key = 0;
		perform(inputNumbers, key, warpperLambda((v, k) -> System.out.println(v / k)));
	}

	private static void perform(int[] inputNumbers, int key, BiConsumer<Integer, Integer> consumer) {
		for (int i : inputNumbers) {
			consumer.accept(i, key);
		}
	}

	private static BiConsumer<Integer, Integer> warpperLambda(BiConsumer<Integer, Integer> consumer) {
		return (v, k) -> {
			try {
				consumer.accept(v, k);
			} catch (Exception e) {
				System.out.println(e);
			}
		};
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *