diff --git a/core-java/src/main/java/com/baeldung/java8/lambda/exceptions/LambdaExceptionWrappers.java b/core-java/src/main/java/com/baeldung/java8/lambda/exceptions/LambdaExceptionWrappers.java new file mode 100644 index 0000000000..64532c8b6f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java8/lambda/exceptions/LambdaExceptionWrappers.java @@ -0,0 +1,57 @@ +package com.baeldung.java8.lambda.exceptions; + +import java.util.function.Consumer; + +public class LambdaExceptionWrappers { + + public static Consumer lambdaWrapper(Consumer consumer) { + return i -> { + try { + consumer.accept(i); + } catch (ArithmeticException e) { + System.err.println("Arithmetic Exception occured : " + e.getMessage()); + } + }; + } + + static Consumer consumerWrapper(Consumer consumer, Class clazz) { + return i -> { + try { + consumer.accept(i); + } catch (Exception ex) { + try { + E exCast = clazz.cast(ex); + System.err.println("Exception occured : " + exCast.getMessage()); + } catch (ClassCastException ccEx) { + throw ex; + } + } + }; + } + + public static Consumer throwingConsumerWrapper(ThrowingConsumer throwingConsumer) { + return i -> { + try { + throwingConsumer.accept(i); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + }; + } + + public static Consumer handlingConsumerWrapper(ThrowingConsumer throwingConsumer, Class exceptionClass) { + return i -> { + try { + throwingConsumer.accept(i); + } catch (Exception ex) { + try { + E exCast = exceptionClass.cast(ex); + System.err.println("Exception occured : " + exCast.getMessage()); + } catch (ClassCastException ccEx) { + throw new RuntimeException(ex); + } + } + }; + } + +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/java8/lambda/exceptions/ThrowingConsumer.java b/core-java/src/main/java/com/baeldung/java8/lambda/exceptions/ThrowingConsumer.java new file mode 100644 index 0000000000..c5860e7f6b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java8/lambda/exceptions/ThrowingConsumer.java @@ -0,0 +1,8 @@ +package com.baeldung.java8.lambda.exceptions; + +@FunctionalInterface +public interface ThrowingConsumer { + + void accept(T t) throws E; + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/java8/lambda/exceptions/LambdaExceptionWrappersTest.java b/core-java/src/test/java/com/baeldung/java8/lambda/exceptions/LambdaExceptionWrappersTest.java new file mode 100644 index 0000000000..7b8ee55a27 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java8/lambda/exceptions/LambdaExceptionWrappersTest.java @@ -0,0 +1,46 @@ +package com.baeldung.java8.lambda.exceptions; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import static com.baeldung.java8.lambda.exceptions.LambdaExceptionWrappers.*; + +public class LambdaExceptionWrappersTest { + + private List integers; + + @Before + public void init() { + integers = Arrays.asList(3, 9, 7, 0, 10, 20); + } + + @Test + public void whenNoExceptionFromLambdaWrapper_thenSuccess() { + integers.forEach(lambdaWrapper(i -> System.out.println(50 / i))); + } + + @Test + public void whenNoExceptionFromConsumerWrapper_thenSuccess() { + integers.forEach(consumerWrapper(i -> System.out.println(50 / i), ArithmeticException.class)); + } + + @Test(expected = RuntimeException.class) + public void whenExceptionFromThrowingConsumerWrapper_thenSuccess() { + integers.forEach(throwingConsumerWrapper(i -> writeToFile(i))); + } + + @Test + public void whenNoExceptionFromHandlingConsumerWrapper_thenSuccess() { + integers.forEach(handlingConsumerWrapper(i -> writeToFile(i), IOException.class)); + } + + void writeToFile(Integer i) throws IOException { + if (i == 0) { + throw new IOException(); // mock IOException + } + } +}