Skip to content

Tunnels up checked exceptions within the stream API without needing try-catch blocks

Notifications You must be signed in to change notification settings

pldilley/lambda-wrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Tunnels up checked exceptions within streams that would usually need a try-catch block, to a wrapper surrounding the stream.

        // BAD ORIGINAL: HOW YOU MIGHT HAVE TO HANDLE AN INLINE EXCEPTION WITHIN A STREAM
        try {
            Object fieldObjs = Arrays.stream(la.getClass().getFields())
                    .map(field -> {
                        try {
                            return field.get(la);
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        }
                    })
                    .collect(Collectors.toList());

            System.out.println(fieldObjs);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
         // GOOD NEW: HOW YOU CAN USE A WRAPPER TO BUBBLE UP THE EXCEPTIONS
         try {
             Object fieldObjs2 = LambdaWrap.withReturn(IllegalAccessException.class,
                     safe -> Arrays.stream(la.getClass().getFields())
                         .map(safe.function(field -> field.get(la)))
                         .collect(Collectors.toList()));

             System.out.println(fieldObjs2);
         } catch (IllegalAccessException e) {
             e.printStackTrace();
         }

About

Tunnels up checked exceptions within the stream API without needing try-catch blocks

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages