Dealing with checked exceptions

Learn how to handle checked exceptions.

When implementing your reactive pipeline, you write lots of functions (java.util.function.Function), consumers (java.util.function.Consumer), suppliers (java.util.function.Supplier) and so on. By default, you cannot throw checked exceptions.

When integrating libraries throwing checked exceptions (like IOException) it’s not very convenient to add a try/catch block and wrap the thrown exception into a runtime exception:

Uni<Integer> uni = item.onItem().transform(i -> {
    try {
        return methodThrowingIoException(i);
    } catch (IOException e) {
        throw new UncheckedIOException(e);

Mutiny provides utilities to avoid having to do this manually. If your operation throws a checked exception, you can use the io.smallrye.mutiny.unchecked.Unchecked wrappers.

For example, if your synchronous transformation uses a method throwing a checked exception, wrap it using Unchecked.function:

Uni<Integer> uni = item.onItem().transform(Unchecked.function(i -> {
    // Can throw checked exception
    return methodThrowingIoException(i);

You can also wrap consumers such as in:

Uni<Integer> uni = item.onItem().invoke(Unchecked.consumer(i -> {
    // Can throw checked exception
    throw new IOException("boom");
Static Import

You can add the following import statement to simplify the usage of the provided methods:

import static io.smallrye.mutiny.unchecked.Unchecked.*;