avatarRaouf Makhlouf

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2692

Abstract

tter) you can write test cases to make sure it is working the way you expect. Best of all, the principal method is now simplified so that the intermediate map operation is only a single line.</p><p id="5e59">In this case, the extracted method catches the checked exception and rethrows it as unchecked. This approach isn’t bad, and in fact, is pretty common, but it does require you to write the extracted method each time. Isn’t there a way to generalize that?</p><p id="10d7">The problem here is that the map method takes a Function, and the apply method in Function does not declare any exceptions. What if you created another functional interface, similar to Function, whose apply method did declare that it throws an exception?</p> <figure id="a4e8"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Raouf25/e1d0b0bb586d52bd0d6eb2f6068d0e7c.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="8e75">The generic parameters T and R in FunctionWithException are just like their counterparts in Function, but the added E parameter extends Exception.

The apply method now takes a T, returns an R, and declares it may throw an E.</p><p id="7e28">Now you can write a method, here called wrapper, that takes an argument of type FunctionWithException and returns a Function:</p> <figure id="d102"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Raouf25/e0c1af2b58a7a9a99b2d716f4cc7a710.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="31f0">The argument to the wrapper method is any FunctionWithException. 

The implementation embeds a try/catch block that catches any exception and rethrows it as an unchecked exception. The return type is a java.util.function. Function, which is the required argument for the map method.</p> <figure id="402b"> <div> <div>

            <iframe class="gist-iframe" src="/gist/Raouf25/7f7657038594d14c387c0e3951b35dd8.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="e099" type="7">Don’t reinvent the wheel!</p><p id="d909">We searched to do efficiently with all Functional interfaces; we didn’t have to start with a blank page; we can use an existent maven dependency for exception management.</p>
 

Options

<figure id="11ce"> <div> <div>
            <iframe class="gist-iframe" src="/gist/Raouf25/851edc2921dac3271d1b1c021651c9cf.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="0446">you find code project on GitHub</p><div id="4386" class="link-block">
      <a href="https://github.com/Raouf25/throwing-function">
        <div>
          <div>
            <h2>Raouf25/throwing-function</h2>
            <div><h3>and adapters Standard java.util.function Functional Interfaces aren't checked-exception-friendly due to the absence of…</h3></div>
            <div><p>github.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*KClti5ncyA5zp4qo)"></div>
          </div>
        </div>
      </a>
    </div><p id="095f">I hope it will help you!</p><p id="5a67">Thank you for your time. I hope you got interesting stuff for you and enjoyed the reading.</p><p id="6c5c">✉<a href="https://softwaretipsandtricks.substack.com/"><b><i> Let’s keep in touch! Sign up for my weekly newsletter</i></b></a></p><p id="4f13"><b>❤ If you liked this post, you might also love:</b></p><div id="0b63" class="link-block">
      <a href="https://readmedium.com/java-8-functional-programming-how-i-do-f11239a0aa90">
        <div>
          <div>
            <h2>Java 8 functional programming, how I do?</h2>
            <div><h3>Since Java 8 is released, we can have another view on the legacy Plain Ordinary Java Objects (or short “POJO”) and work…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*DtYDDN1tOLDnlHWawHI-zQ.png)"></div>
          </div>
        </div>
      </a>
    </div><div id="2f1c" class="link-block">
      <a href="https://readmedium.com/useful-git-alias-926f27a27f92">
        <div>
          <div>
            <h2>Useful git alias</h2>
            <div><h3>It’s well known That developers want to optimize and simplify repetitive tasks. We like to make shortcuts, type fewer…</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*L7xOvjD6s_6AQHOW)"></div>
          </div>
        </div>
      </a>
    </div></article></body>

Java 8, How to handle exceptions in a stream?

Moving to Java 8 adopt the new approach pretty easily, and the resulting code tends to be shorter and easier to follow unless you have to deal with exceptions in a stream.

Photo by Math on Unsplash

Let’s consider the case of John, who was a developer. He’s been told by his boss that the customer wants to migrate this code to Java 8 :

John is thinking: Ah, I get it now I can change the for-loop by a stream, and I catch the exception. So easy!

This works but makes the code harder to read and understand. Ideally, you would like to keep each intermediate operation as a single line when writing pipeline code.

John says: A good alternative is to extract the function argument to map into a method of its own.

Developers have been writing code like the extractInputStream method for years. It looks familiar, is easy to understand, and (even better) you can write test cases to make sure it is working the way you expect. Best of all, the principal method is now simplified so that the intermediate map operation is only a single line.

In this case, the extracted method catches the checked exception and rethrows it as unchecked. This approach isn’t bad, and in fact, is pretty common, but it does require you to write the extracted method each time. Isn’t there a way to generalize that?

The problem here is that the map method takes a Function, and the apply method in Function does not declare any exceptions. What if you created another functional interface, similar to Function, whose apply method did declare that it throws an exception?

The generic parameters T and R in FunctionWithException are just like their counterparts in Function, but the added E parameter extends Exception. The apply method now takes a T, returns an R, and declares it may throw an E.

Now you can write a method, here called wrapper, that takes an argument of type FunctionWithException and returns a Function:

The argument to the wrapper method is any FunctionWithException. The implementation embeds a try/catch block that catches any exception and rethrows it as an unchecked exception. The return type is a java.util.function. Function, which is the required argument for the map method.

Don’t reinvent the wheel!

We searched to do efficiently with all Functional interfaces; we didn’t have to start with a blank page; we can use an existent maven dependency for exception management.

you find code project on GitHub

I hope it will help you!

Thank you for your time. I hope you got interesting stuff for you and enjoyed the reading.

Let’s keep in touch! Sign up for my weekly newsletter

❤ If you liked this post, you might also love:

Programming
Java
Exception
Stream
Interview
Recommended from ReadMedium