Skip to content

Commit 0aa4699

Browse files
vivekkr12pivovarit
authored andcommitted
BAEL-580 handling exceptions in lambda expressions (eugenp#1004)
1 parent deb694b commit 0aa4699

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.java8.lambda.exceptions;
2+
3+
import java.util.function.Consumer;
4+
5+
public class LambdaExceptionWrappers {
6+
7+
public static Consumer<Integer> lambdaWrapper(Consumer<Integer> consumer) {
8+
return i -> {
9+
try {
10+
consumer.accept(i);
11+
} catch (ArithmeticException e) {
12+
System.err.println("Arithmetic Exception occured : " + e.getMessage());
13+
}
14+
};
15+
}
16+
17+
static <T, E extends Exception> Consumer<T> consumerWrapper(Consumer<T> consumer, Class<E> clazz) {
18+
return i -> {
19+
try {
20+
consumer.accept(i);
21+
} catch (Exception ex) {
22+
try {
23+
E exCast = clazz.cast(ex);
24+
System.err.println("Exception occured : " + exCast.getMessage());
25+
} catch (ClassCastException ccEx) {
26+
throw ex;
27+
}
28+
}
29+
};
30+
}
31+
32+
public static <T> Consumer<T> throwingConsumerWrapper(ThrowingConsumer<T, Exception> throwingConsumer) {
33+
return i -> {
34+
try {
35+
throwingConsumer.accept(i);
36+
} catch (Exception ex) {
37+
throw new RuntimeException(ex);
38+
}
39+
};
40+
}
41+
42+
public static <T, E extends Exception> Consumer<T> handlingConsumerWrapper(ThrowingConsumer<T, E> throwingConsumer, Class<E> exceptionClass) {
43+
return i -> {
44+
try {
45+
throwingConsumer.accept(i);
46+
} catch (Exception ex) {
47+
try {
48+
E exCast = exceptionClass.cast(ex);
49+
System.err.println("Exception occured : " + exCast.getMessage());
50+
} catch (ClassCastException ccEx) {
51+
throw new RuntimeException(ex);
52+
}
53+
}
54+
};
55+
}
56+
57+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.java8.lambda.exceptions;
2+
3+
@FunctionalInterface
4+
public interface ThrowingConsumer<T, E extends Exception> {
5+
6+
void accept(T t) throws E;
7+
8+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.java8.lambda.exceptions;
2+
3+
import java.io.IOException;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import static com.baeldung.java8.lambda.exceptions.LambdaExceptionWrappers.*;
11+
12+
public class LambdaExceptionWrappersTest {
13+
14+
private List<Integer> integers;
15+
16+
@Before
17+
public void init() {
18+
integers = Arrays.asList(3, 9, 7, 0, 10, 20);
19+
}
20+
21+
@Test
22+
public void whenNoExceptionFromLambdaWrapper_thenSuccess() {
23+
integers.forEach(lambdaWrapper(i -> System.out.println(50 / i)));
24+
}
25+
26+
@Test
27+
public void whenNoExceptionFromConsumerWrapper_thenSuccess() {
28+
integers.forEach(consumerWrapper(i -> System.out.println(50 / i), ArithmeticException.class));
29+
}
30+
31+
@Test(expected = RuntimeException.class)
32+
public void whenExceptionFromThrowingConsumerWrapper_thenSuccess() {
33+
integers.forEach(throwingConsumerWrapper(i -> writeToFile(i)));
34+
}
35+
36+
@Test
37+
public void whenNoExceptionFromHandlingConsumerWrapper_thenSuccess() {
38+
integers.forEach(handlingConsumerWrapper(i -> writeToFile(i), IOException.class));
39+
}
40+
41+
void writeToFile(Integer i) throws IOException {
42+
if (i == 0) {
43+
throw new IOException(); // mock IOException
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)