Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

throwing-function

> 编程语言
Open source

Checked Exceptions-enabled Java 8+ functional interfaces + adapters

392 stars0 likes1 views
WebsiteGitHub

About

Checked Exceptions-enabled Java 8+ functional interfaces + adapters

throwing-function

Checked-exception-enabled Java functional interfaces — a zero-dependency drop-in for java.util.function.

The Problem

Java's java.util.function interfaces don't support checked exceptions, turning clean one-liners into verbose try-catch blocks:

java
// what you want
.map(path -> new URI(path))

// what Java forces you to write
.map(path -> {
    try {
        return new URI(path);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
})

The Solution

throwing-function provides checked-exception-aware variants of all standard functional interfaces:

java
ThrowingFunction toUri = URI::new;

And adapters to integrate them into standard Java APIs:

java
stream.map(ThrowingFunction.unchecked(URI::new))
      .forEach(System.out::println);

Functional Interfaces

Interface Functional method
ThrowingFunction R apply(T t) throws E
ThrowingBiFunction R apply(T1 t1, T2 t2) throws E
ThrowingUnaryOperator T apply(T t) throws E
ThrowingBinaryOperator T apply(T t1, T t2) throws E
ThrowingConsumer void accept(T t) throws E
ThrowingBiConsumer void accept(T t, U u) throws E
ThrowingSupplier T get() throws E
ThrowingPredicate boolean test(T t) throws E
ThrowingBiPredicate boolean test(T t, U u) throws E
ThrowingRunnable void run() throws E
ThrowingToLongFunction long applyAsLong(T t) throws E

Adapters

Each interface provides static adapter methods to bridge Throwing* instances into standard java.util.function types.

unchecked — wrap in CheckedException

Wraps the checked exception in a CheckedException (a RuntimeException subclass) and rethrows it. Available on all interfaces.

java
stream.map(ThrowingFunction.unchecked(URI::new))
      .forEach(System.out::println);

sneaky — rethrow without wrapping

Rethrows the checked exception as-is, bypassing the compiler's checked-exception enforcement via type erasure. Available on all interfaces.

java
stream.map(ThrowingFunction.sneaky(URI::new))
      .forEach(System.out::println);

lifted / optional — return Optional

Returns the result wrapped in an Optional, or Optional.empty() on exception. No exception is propagated.

  • ThrowingFunction.lifted(f) → Function>
  • ThrowingBiFunction.optional(f) → BiFunction>
  • ThrowingSupplier.optional(s) → Supplier>
java
stream.map(ThrowingFunction.lifted(URI::new))  // Stream>
      .forEach(System.out::println);

Installation

Maven

xml

    com.pivovarit
    throwing-function
    1.6.1

Gradle

groovy
implementation 'com.pivovarit:throwing-function:1.6.1'

Used by

  • bbottema/simple-java-mail — Simple Java Mail, one of the most widely used Jakarta Mail / SMTP wrappers in the Java ecosystem.

Dependencies

None. Implemented using core Java libraries only.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Javaexception-handlingfunctional-programminghacktoberfestjava-8

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言