Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

Exceptions in Java lambdas – DEV Community ðŸ‘Đ‍ðŸ’ŧðŸ‘Ļ‍ðŸ’ŧ

Java launched the idea of checked exceptions. The concept of forcing builders to handle exceptions was revolutionary in comparison with the earlier approaches.

These days, Java stays the one widespread language to supply checked exceptions. For instance, each exception in Kotlin is unchecked.

Even in Java, new options are at odds with checked exceptions:the signature of Java’s built-in useful interfaces would not use exceptions.It results in cumbersome code when one integrates legacy code in lambdas.It is evident in Streams.

On this publish, I might wish to dive deeper into how one can handle such issues.

The issue within the code

Here is a pattern code as an instance the problem:

Stream.of("java.lang.String", "ch.frankel.weblog.Dummy", "java.util.ArrayList")
      .map(it -> new ForNamer().apply(it))                                     // 1
Enter fullscreen mode

Exit fullscreen mode

  1. Does not compile: must catch the checked ClassNotFoundException

We should add a attempt/catch block to repair the compilation situation.

Stream.of("java.lang.String", "ch.frankel.weblog.Dummy", "java.util.ArrayList")
      .map(it -> {
          attempt {
              return Class.forName(it);
          } catch (ClassNotFoundException e) {
              throw new RuntimeException(e);
Enter fullscreen mode

Exit fullscreen mode

Including the block defeats the aim of easy-to-read pipelines.

Encapsulate the attempt/catch block into a category

To get the readability again, we have to refactor the code to introduce a brand new class. IntelliJ IDEA even suggests a file:

var forNamer = new ForNamer();                                                // 1
Stream.of("java.lang.String", "ch.frankel.weblog.Dummy", "java.util.ArrayList")
      .map(forNamer::apply)                                                   // 2

file ForNamer() implements Operate<String, Class<?>> {

    public Class<?> apply(String string) {
        attempt {
            return Class.forName(string);
        } catch (ClassNotFoundException e) {
            return null;
Enter fullscreen mode

Exit fullscreen mode

  1. Create a single file object
  2. Reuse it

Attempting with Lombok

Mission Lombok is a compile-time annotation processor that generates extra bytecode. One makes use of the correct annotation and will get the consequence with out having to put in writing boilerplate code.

Mission Lombok is a java library that routinely plugs into your editor and construct instruments, spicing up your java. By no means write one other getter or equals technique once more, with one annotation your class has a completely featured builder, Automate your logging variables, and far more.

Project Lombok

Lombok affords the @SneakyThrow annotation:it permits one to throw checked exceptions with out declaring them in a single’s technique signature.But, it would not work for an present API for the time being.

In the event you’re a Lombok person, notice that there is an opened GitHub issue with the standing parked.

Commons Lang to the rescue

Apache Commons Lang is an age-old venture.It was widespread on the time because it provided utilities that would have been a part of the Java API however weren’t.It was a significantly better various than reinventing your DateUtils and StringUtils in each venture.Whereas researching this publish, I found it’s nonetheless usually maintained with nice APIs.One among them is the Failable API.

The API consists of two components:

  1. A wrapper round a Stream
  2. Pipeline strategies whose signature accepts exceptions

Here is a small excerpt:

The code lastly turns into what we anticipated from the start:

Stream<String> stream = Stream.of("java.lang.String", "ch.frankel.weblog.Dummy", "java.util.ArrayList");
        .map(Class::forName)                                                  // 1
Enter fullscreen mode

Exit fullscreen mode

Fixing compile-time errors isn’t sufficient

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

  • Throw on the first exception
  • Discard exceptions
  • Combination each courses 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 ability of Vavr. Vavr is a library that brings the ability of Useful Programming to the Java language:

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


Think about that we would like a pipeline that collects each exceptions and courses. Here is an excerpt of the API that describes a number of constructing blocks.

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());
Enter fullscreen mode

Exit fullscreen mode

  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 Non-obligatory as an alternative
  3. Act relying on whether or not the Both incorporates an exception, left, or the anticipated consequence, proper

Up to now, we have now stayed on the earth of Java Streams. It really works as anticipated till the forEach, which does not 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 consequence = Stream.of("java.lang.String", "ch.frankel.weblog.Dummy", "java.util.ArrayList")
        .partition(Both::isLeft)                                              // 1
        .map1(left -> left.map(Both::getLeft))                                // 2
        .map2(proper -> proper.map(Both::get));                                 // 3

consequence._1().forEach(it -> System.out.println("not discovered: " + it.getMessage())); // 4
consequence._2().forEach(it -> System.out.println("class: " + it.getName()));        // 4
Enter fullscreen mode

Exit fullscreen mode

  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 precise stream from a Stream of Both to a Stream of Class
  4. Do no matter we would like


Java’s preliminary design made loads of use of checked exceptions. The evolution of programming languages proved that it was not a good suggestion.

Java streams do not play properly with checked exceptions.The code essential to combine the latter into the previous would not look good.To get well the readability we count on of streams, we will depend on Apache Commons Lang.

The compilation represents solely a tiny fraction of the problem.We usually need to act upon the exceptions, not cease the pipeline or ignore exceptions.On this case, we will leverage the Vavr library, which affords an much more useful strategy.

You could find the supply code for this publish on GitHub:

To go additional:

Initially revealed at A Java Geek on October 16th, 2022

Add a Comment

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?