By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: How to Make Your Legacy Exception-Throwing Code Compatible With Lambdas | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > How to Make Your Legacy Exception-Throwing Code Compatible With Lambdas | HackerNoon
Computing

How to Make Your Legacy Exception-Throwing Code Compatible With Lambdas | HackerNoon

News Room
Last updated: 2026/01/22 at 6:52 PM
News Room Published 22 January 2026
Share
How to Make Your Legacy Exception-Throwing Code Compatible With Lambdas | HackerNoon
SHARE

Java’s checked exceptions were a massive improvement over C’s error-handling mechanism. As time passed and experience accumulated, we collectively concluded that we weren’t there yet. However, Java’s focus on stability has kept checked exceptions in its existing API.

Java 8 brought lambdas after the “checked exceptions are great” trend. None of the functional interface methods accepts a checked exception. In this post, I will demonstrate three different approaches to making your legacy exception-throwing code compatible with lambdas.

The problem, in code.

Consider a simple exception-throwing method. n

public class Foo {
    public String throwing(String input) throws IOException {
        return input;                          //1
    }
}
  1. The body is there for compilation purposes; its exact content is irrelevant.

The method accepts a String and returns a String. It has the shape of a Function<I, O>, so we can use it as such: n

var foo = new Foo();
List.of("One", "Two").stream()
    .map(string -> foo.throwing(string))
    .toList();

The above code fails with a compilation error: n

unreported exception IOException; must be caught or declared to be thrown
            .map(string -> foo.throwing(string)).toList();
                                       ^
1 error

To fix the error, we need to wrap the throwing code in a try-catch block: n

List.of("One", "Two").stream()
    .map(string -> {
        try {
            return foo.throwing(string);
        } catch (IOException e) {
            return "";
        }
    }).toList();

At this point, the code compiles, but defeats the main purpose of lambdas: being concise and readable.

A better approach.

We can definitely improve the design by modeling a Function with a throwing apply(). n

interface ThrowingFunction<I, O, E extends Exception> {
    O apply(I i) throws E;
}

We can then provide a wrapper to transform such a throwing Function into a regular Function. n

class LambdaUtils {
    public static <I, O, E extends Exception> Function<I, O> safeApply(ThrowingFunction<I, O, E> f) {
        return input -> {
            try {
                return f.apply(input);
            } catch (Exception e) {
                return "";
            }
        };
    }
}

With the above, the calling code can be improved like this: n

var foo = new Foo();
List.of("One", "Two").stream()
    .map(string -> LambdaUtils.safeApply(foo::throwing))     //1
    .toList();
  1. Concise code again

Libraries to the rescue.

The most straightforward way to call exception-throwing code in a lambda involves using a library. Two libraries that I know of provide this capability:

  • Apache Commons Lang 3
  • Vavr

Here’s how we can rewrite the above code using Commons Lang 3 code: n

var foo = new Foo();
FailableFunction<String, String, IOException> throwingFunction = foo::throwing; //1
List.of("One", "Two").stream()
    .map(throwingFunction)
    .recover(e -> "")                                                           //2
    .toList();
  1. Commons Lang 3 models a throwing Function
  2. recover() mimics the value set in the previous catch block

The libraries improve upon my debatable design above, but the idea stays the same.

The decision to roll out your own or use a library depends on a variety of factors that go beyond this post. Here are some criteria to help you.

Suppressing Checked Exceptions

Checked exceptions are a compile-time concern. The Java Language Specification states:

A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method (§8.4.6) or constructor (§8.8.5) must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.

— 11.2 Compile-Time Checking of Exceptions

We could potentially hook into the compiler to prevent this check via a compiler plugin. Or find a library that does. That’s when Manifold enters the scene.

Manifold is a Java compiler plugin. Use it to supplement your Java projects with highly productive features.

Powerful language enhancements improve developer productivity.

  • Extension methods
  • True delegation
  • Properties
  • Optional parameters (New!)
  • Tuple expressions
  • Operator overloading
  • Unit expressions
  • A Java template engine
  • A preprocessor
  • …and more

— What is Manifold

Disclaimer: I don’t advocate for using Manifold. It makes the language you work with different from Java. At this point, you’d be better off using Kotlin directly.

Using Manifold to suppress checked exceptions is a two-step process. First, we add the Manifold runtime to the project: n

<dependency>
    <groupId>systems.manifold</groupId>
    <artifactId>manifold-rt</artifactId>
    <version>${manifold.version}</version>
    <scope>provided</scope>
</dependency>

Then, we configure the compiler plugin: n

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <encoding>UTF-8</encoding>
                <compilerArgs>
                    <arg>-Xplugin:Manifold</arg>
                </compilerArgs>
                <annotationProcessorPaths>
                    <path>
                        <groupId>systems.manifold</groupId>
                        <artifactId>manifold-exceptions</artifactId>
                        <version>${manifold.version}</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

At this point, we can treat checked exceptions as unchecked. n

var foo = new Foo();
List.of("One", "Two").stream()
        .map(string -> foo.throwing(string))       //1
        .toList();
  1. Compile with no issue

Conclusion

In this post, I tackled the issue of integrating checked exceptions with lambdas in Java. I listed several options: the try-catch block, the throwing function, the library, and Manifold. I hope you can find one that suits your context among them.

To go further:

  • Apache Commons Lang 3
  • Vavr
  • Exceptions in Java Lambda Expressions
  • Exceptions in Lambda Expression Using Vavr
  • Say Goodbye to Checked Exceptions
  • Revisiting Resolving the Scourge of Java’s Checked Exceptions

Originally published at A Java Geek on January 18th, 2026

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article PETA Proposes Groundhog Hologram for Future Groundhog Days PETA Proposes Groundhog Hologram for Future Groundhog Days
Next Article Vimeo lays off ‘large portion’ of staff after Bending Spoons buyout Vimeo lays off ‘large portion’ of staff after Bending Spoons buyout
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Capital One to acquire corporate card provider Brex in .15B deal –  News
Capital One to acquire corporate card provider Brex in $5.15B deal – News
News
Bizarre new life form discovered in UK 370million years after extinction
Bizarre new life form discovered in UK 370million years after extinction
News
Elegoo&apos;s New 3D Printer Is the First Centauri With a Color System Attached
Elegoo's New 3D Printer Is the First Centauri With a Color System Attached
News
Artificial intelligence and the emotional pulse of work
Artificial intelligence and the emotional pulse of work
Mobile

You Might also Like

Educational Byte: What Are Fed Rates and Why Do They Affect Crypto Prices? | HackerNoon
Computing

Educational Byte: What Are Fed Rates and Why Do They Affect Crypto Prices? | HackerNoon

6 Min Read
How to Add the AWS WAF CAPTCHA to an Angular Application | HackerNoon
Computing

How to Add the AWS WAF CAPTCHA to an Angular Application | HackerNoon

8 Min Read
AlphaTON Launches Claude Connector Powered By TON And Telegram | HackerNoon
Computing

AlphaTON Launches Claude Connector Powered By TON And Telegram | HackerNoon

8 Min Read
Reports: Amazon’s latest layoffs could begin next week
Computing

Reports: Amazon’s latest layoffs could begin next week

4 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?