Skip to content

io microsphere lang function ThrowableSupplier

github-actions[bot] edited this page Mar 24, 2026 · 3 revisions

ThrowableSupplier

Type: Interface | Module: microsphere-java-core | Package: io.microsphere.lang.function | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/lang/function/ThrowableSupplier.java

Overview

A functional interface similar to Supplier, but allows the get() method to throw a Throwable. This is useful for functional constructs where operations may throw checked exceptions that need to be handled or rethrown.

Example Usage

`// Using ThrowableSupplier to read a file content
ThrowableSupplier fileReader = () -> {
    Path path = Paths.get("example.txt");
    return Files.readString(path);
`;

// Execute with default exception handling (converts to RuntimeException)
String content = fileReader.execute();

// Execute with custom exception handling
String contentWithHandler = fileReader.execute(ex -> {
    System.err.println("Error reading file: " + ex.getMessage());
    return "default content";
});
}

This interface provides convenience methods to execute the supplier and handle exceptions using a custom handler, making it easier to work with functional patterns in environments where exceptions must be managed.

Declaration

public interface ThrowableSupplier<T>

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.2.2-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

// Using ThrowableSupplier to read a file content
ThrowableSupplier<String> fileReader = () -> {
    Path path = Paths.get("example.txt");
    return Files.readString(path);
};

// Execute with default exception handling (converts to RuntimeException)
String content = fileReader.execute();

// Execute with custom exception handling
String contentWithHandler = fileReader.execute(ex -> {
    System.err.println("Error reading file: " + ex.getMessage());
    return "default content";
});

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.lang.function.ThrowableSupplier;

See Also

  • Supplier
  • Throwable

This documentation was auto-generated from the source code of microsphere-java.

Home

annotation-processor

java-annotations

java-core

java-test

jdk-tools

lang-model

Clone this wiki locally