The Java 8 lambda syntax and functional interfaces have been a productivity boost for Java developers. But there is one drawback to functional interfaces. None of the them as currently defined in Java 8 declare any checked exceptions. This leaves the developer at odds on how best to handle checked exceptions. This post will present one option for handling checked exceptions in functional interfaces. We will use use the
Function in our example, but the pattern should apply to any of the functional interfaces.
Example of a Function with a Checked Exception
Here’s an example from a recent side-project using a
Function to open a directory in Lucene. As expected, opening a directory for writing/searching throws an
1 2 3 4 5 6 7