Exception Handling in Eclipse Collections

Donald Raab
Sep 17 · 4 min read

Some strategies for handling exceptions in Eclipse Collections before and after Java 8

Image for post
Image for post
Handling checked exceptions with iteration patterns and lambdas can feel like walking on ice

The Inspiration for this post

The post reminded me of the exception handling support we have had in Eclipse Collections for a few years now. I thought I would show some simple examples of handling Checked Exceptions using Eclipse Collections before and after Java 8. I will also show examples that will work with Java Streams.

The Dependable Appendable

Image for post
Image for post

If I create a MutableList of String, and attempt to use append as a method reference with the forEach method, my IDE gives me a warning saying there is an Unhandled Exception: java.io.IOException.

Image for post
Image for post
Unhandled Exception: java.io.IOException

As it turns out, StringBuilder doesn’t actually throw a java.io.IOException in its method signature for the append method. So if I change the type to StringBuilder, the exception will go away.

Image for post
Image for post
The StringBuilder accept method doesn’t throw java.io.IOException

The same would be true if I changed the code to use StringBuffer. However, all Appendable implementations that deal with files will most likely throw the exception. So if I use the interface, and not specific implementations, I will have to handle the exception. The ugly way of handling the exception prior to Java 8, might look as follows.

Image for post
Image for post

This is a common pattern, catching a checked exception and throwing a RuntimeException. It was common enough for us before Java 8 that we had specific “Checked” versions of the different functional interfaces in Eclipse Collections. There is a package named “checked” in each of the functional interface packages (function, predicate, procedure). Using a CheckedProcedure, we could simplify the code as follows:

Image for post
Image for post
How to deal with checked exceptions before Java 8

If an exception is thrown by the Appendable, the exception will be caught and if it is a RuntimeException, it will be bubbled up. If it is a checked exception, a RuntimeException will be created and thrown with the checked exception set as the cause.

After Java 8, we introduced new interfaces to deal with Checked Exceptions. For each Functional Interface type (Function, Predicate, Procedure) there is a corresponding “throwing” type (ThrowingFunction, ThrowingPredicate, ThrowingProcedure). There are also corresponding methods on the factories for the the functional interfaces (Functions.throwing, Predicates.throwing, Procedures.throwing). Using the combination of these features with Java 8 or above we can now write the following:

Image for post
Image for post
Dealing with checked exceptions with Java 8 or above

You can also use the same approach with Java Collections and Java Streams.

Image for post
Image for post
Using Java 8+ forEach method on Iterable which takes Consumer

This code works, because the Procedure interface in Eclipse Collections extends the Consumer interface. The code will work using as a Stream as well.

Image for post
Image for post
Using Stream forEach which also takes Consumer

Throwing your own RuntimeExceptions

Image for post
Image for post
The Function2 takes the current element, the exception caught and the RuntimeException type to rethrow

Let’s say I want to rethrow an UndependableAppendableException in case an IOException is caught in the call to append. The code might look as follows:

Image for post
Image for post
The UndependableAppendableException constructor matches the Function2 signature defined in throwing

Notice, I kept the code here working with Java Stream. It would work the same using forEach on a List or a MutableList from Eclipse Collections.

But wait, what about…

I am a Project Lead and Committer for the Eclipse Collections OSS project at the Eclipse Foundation. Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.

Javarevisited

Medium’s largest Java publication, followed by 7100+ programmers. Follow to join our community.

Donald Raab

Written by

Java Champion. Creator of the Eclipse Collections OSS Java library (http://www.eclipse.org/collections/). Inspired by Smalltalk. Opinions are my own.

Javarevisited

A humble place to learn Java and Programming better.

Donald Raab

Written by

Java Champion. Creator of the Eclipse Collections OSS Java library (http://www.eclipse.org/collections/). Inspired by Smalltalk. Opinions are my own.

Javarevisited

A humble place to learn Java and Programming better.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store