百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
T

throwing-function

> 编程语言
开源

支持检查异常的 Java 8+ 功能接口 + 适配器

392 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

支持检查异常的 Java 8+ 功能接口 + 适配器

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 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javaexception-handlingfunctional-programminghacktoberfestjava-8

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

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