DEV Community

loading...

Discussion on: I hate checked exceptions

Collapse
tiguchi profile image
Thomas Iguchi

You can define your own BiConsumer interface that throws checked exceptions as follows:

@FunctionalInterface
public interface CheckedBiConsumer<T, U, E extends Exception> {
    void accept(T arg1, U arg2) throws E;
}

And also define a CheckedRunnable that propagates the exception thrown by the CheckedBiConsumer:

@FunctionalInterface
public interface CheckedRunnable<E extends Exception> {
    void run() throws E;
}

That way you can leave it up to the code executing the runnable what to do with the checked exception.

Collapse
chochos profile image
Enrique Zamudio Author

Tanks for the tip! Creating the CheckedBiConsumer does the trick indeed. I can't use the second one because the code I'm extending already defines that I need to return a standard Runnable though, but that's not a problem.