Home

Awesome

Checked-Exceptions-enabled Java 8+ Functional Interfaces

Build against JDKs License Maven Central Version

Stargazers over time

Overview

Java’s standard java.util.function interfaces are not compatible with checked exceptions. This leads to verbose and cluttered code, requiring manual try-catch blocks for exception handling, which makes one-liners like this:

path -> new URI(path)

become as verbose as:

path -> {
    try {
        return new URI(path);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}

This library introduces checked-exception-enabled functional interfaces, like ThrowingFunction, allowing cleaner, more concise code. You can now handle exceptions in functional pipelines without sacrificing readability:

ThrowingFunction<String, URI, URISyntaxException> toUri = URI::new;

Using the ThrowingFunction#unchecked adapter, this can be seamlessly integrated into standard streams:

...stream()
  .map(unchecked(URI::new)) // static import of ThrowingFunction#unchecked
  .forEach(System.out::println);

This eliminates the need for bulky try-catch blocks within stream operations:

 ...stream().map(path -> {
     try {
         return new URI(path);
     } catch (URISyntaxException e) {
         throw new RuntimeException(e);
     }}).forEach(System.out::println);

Key Features

Core API

Functional Interfaces

Adapters

Transforms a ThrowingFunction instance into a standard java.util.function.Function by wrapping checked exceptions in a RuntimeException and rethrowing them.

Transforms a ThrowingFunction instance into a regular Function returning result wrapped in an Optional instance.

Returns Throwing(Predicate|Supplier|Consumer) instance as a new ThrowingFunction instance.

Maven Central

<dependency>
    <groupId>com.pivovarit</groupId>
    <artifactId>throwing-function</artifactId>
    <version>1.6.1</version>
</dependency>
Gradle
compile 'com.pivovarit:throwing-function:1.6.1'

Dependencies

None - the library is implemented using core Java libraries.

Version history

1.6.1 (25-09-2024)

1.6.0 (24-09-2024)

1.5.1 (06-05-2020)

1.5.0 (26-01-2019)