Chapter 10. Functional Exception Handling
As much as we would like to write perfect and error-free code, it’s an almost impossible endeavor. That’s why we need a way to deal with inevitable problems in our code. Java’s mechanism of choice to deal with such disruptive and abnormal control flow conditions is Exceptions.
Exception handling can be tricky, even in imperative and OO code. Combining Exceptions with a functional approach, however, can be a real challenge because the techniques are fraught with considerations and requirements. Although there are third-party libraries that can assist with this, you may not want to rely solely on them in the long term, incurring technical debt due to a new dependency, instead of adapting to a more functional approach overall.
This chapter will show you the different kinds of Exceptions and their impact on functional programming with lambdas. You will learn how to handle Exceptions in lambdas as well as alternative ways to approach control flow disruptions in a functional context.
Java Exception Handling in a Nutshell
In general, an Exception is a special event that happens during the execution of a program that disrupts the normal flow of instructions. This concept is present in many different programming languages, not only in Java, and traces back to the origins of Lisp.1
The actual way of handling Exceptions depends on the language.
The try-catch block
Java’s mechanism of choice is the try-catch block, which is an integral element ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access