BAEL-580 handling exceptions in lambda expressions (#1004)
This commit is contained in:
		
							parent
							
								
									deb694bc88
								
							
						
					
					
						commit
						0aa46996a6
					
				| @ -0,0 +1,57 @@ | |||||||
|  | package com.baeldung.java8.lambda.exceptions; | ||||||
|  | 
 | ||||||
|  | import java.util.function.Consumer; | ||||||
|  | 
 | ||||||
|  | public class LambdaExceptionWrappers { | ||||||
|  | 
 | ||||||
|  |     public static Consumer<Integer> lambdaWrapper(Consumer<Integer> consumer) { | ||||||
|  |         return i -> { | ||||||
|  |             try { | ||||||
|  |                 consumer.accept(i); | ||||||
|  |             } catch (ArithmeticException e) { | ||||||
|  |                 System.err.println("Arithmetic Exception occured : " + e.getMessage()); | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     static <T, E extends Exception> Consumer<T> consumerWrapper(Consumer<T> consumer, Class<E> 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 <T> Consumer<T> throwingConsumerWrapper(ThrowingConsumer<T, Exception> throwingConsumer) { | ||||||
|  |         return i -> { | ||||||
|  |             try { | ||||||
|  |                 throwingConsumer.accept(i); | ||||||
|  |             } catch (Exception ex) { | ||||||
|  |                 throw new RuntimeException(ex); | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     public static <T, E extends Exception> Consumer<T> handlingConsumerWrapper(ThrowingConsumer<T, E> throwingConsumer, Class<E> 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); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -0,0 +1,8 @@ | |||||||
|  | package com.baeldung.java8.lambda.exceptions; | ||||||
|  | 
 | ||||||
|  | @FunctionalInterface | ||||||
|  | public interface ThrowingConsumer<T, E extends Exception> { | ||||||
|  | 
 | ||||||
|  |     void accept(T t) throws E; | ||||||
|  | 
 | ||||||
|  | } | ||||||
| @ -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<Integer> 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 | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user