Checked Exception Streams (ChkStreams) adds checked exception support to the Java 8 Stream API.
ChkStreams is not an official Google product.
Have you ever wanted to write something like this, but been foiled by the
dreaded Unhandled exception type IOException
?
filenames.parallelStream()
.map(Files::readFully)
.collect(toList());
Be foiled no more!
ChkStreams.of(filenames.parallelStream())
.canThrow(IOException.class)
.map(Files::readFully)
.collect(toList());
ChkStreams extends the existing Java 8 Stream API. To use it, simply:
- Wrap any
Stream
usingChkStreams.of(Stream)
to get aChkStream
. - Declare one or more checked exceptions by calling
ChkStream#canThrow(Exception)
, so that subsequent stream operations will allow thatException
. - Use the same Stream API you're used to (except now your lambdas can throw the declared exceptions!)
- Handle the checked exceptions in the usual way (catch or declare thrown) in any method that invokes a terminal operation on the stream.
- Adds checked exception support to the familiar Streams API!
- Exceptions are enforced by the compiler in the usual way, and need only be handled when invoking terminal operations.
- Can be converted to/from regular Java
Stream
s (SeeChkStreams#of(Stream)
andChkStream#toStream()
). Regular streams obtained fromChkStream#toStream()
will throw unchecked exceptions (specificallyChkStreamWrappedException
) when checked exceptions occur in the stream. - All primitive specializations of
Stream
are supported without boxing (SeeChkStreams#ofInt(IntStream)
). - (Optional) support for the
StreamSupport backport and
Retrolambda. Enjoy the power of
ChkStream
on Java 6+ and Android! - May cause you to barf rainbows.
- A maximum of 5 checked exceptions may be added to a
ChkStream
. - No interfaces in common with
Stream
or betweenChkStream
s with different numbers of exceptions.
This project is licensed under the GNU GPLv2 with Classpath Exception, which is the same license as OpenJDK itself.