Thursday, May 2, 2024
HomeJavaExceptions in lambdas

Exceptions in lambdas


The earlier code throws a ClassNotFoundException wrapped in an UndeclaredThrowableException at runtime. We happy the compiler, however we now have no solution to specify the anticipated conduct:

  • Throw on the first exception
  • Discard exceptions
  • Mixture each lessons and exceptions so we will act upon them on the ultimate stage of the pipeline
  • One thing else

To realize this, we will leverage the facility of Vavr. Vavr is a library that brings the facility of Purposeful Programming to the Java language:

Vavr core is a practical library for Java. It helps to scale back the quantity of code and to extend the robustness. A primary step in direction of practical programming is to start out pondering in immutable values. Vavr offers immutable collections and the mandatory features and management buildings to function on these values. The outcomes are stunning and simply work.

Think about that we wish a pipeline that collects each exceptions and lessons. Right here’s an excerpt of the API that describes a number of constructing blocks.

vavr api

Determine 2. Vavr API excerpt

It interprets into the next code:

Stream.of("java.lang.String", "ch.frankel.weblog.Dummy", "java.util.ArrayList")
      .map(CheckedFunction1.liftTry(Class::forName))                          (1)
      .map(Attempt::toEither)                                                     (2)
      .forEach(e -> {
          if (e.isLeft()) {                                                   (3)
              System.out.println("not discovered:" + e.getLeft().getMessage());
          } else {
              System.out.println("class:" + e.get().getName());
          }
      });

1 Wrap the decision right into a Vavr Attempt
2 Remodel the Attempt into an Both to maintain the exception. If we had not been , we may have used an Elective as a substitute
3 Act relying on whether or not the Both accommodates an exception, left, or the anticipated end result, proper

To date, we now have stayed on the planet of Java Streams. It really works as anticipated till the forEach, which doesn’t look “good”.

Vavr does present its personal Stream class, which mimics the Java Stream API and provides extra options. Let’s use it to rewrite the pipeline:

var end result = Stream.of("java.lang.String", "ch.frankel.weblog.Dummy", "java.util.ArrayList")
        .map(CheckedFunction1.liftTry(Class::forName))
        .map(Attempt::toEither)
        .partition(Both::isLeft)                                              (1)
        .map1(left -> left.map(Both::getLeft))                                (2)
        .map2(proper -> proper.map(Both::get));                                 (3)

end result._1().forEach(it -> System.out.println("not discovered: " + it.getMessage())); (4)
end result._2().forEach(it -> System.out.println("class: " + it.getName()));        (4)

1 Partition the Stream of Both in a tuple of two Stream
2 Flatten the left stream from a Stream of Both to a Stream of Throwable
3 Flatten the correct stream from a Stream of Both to a Stream of Class
4 Do no matter we wish
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments