Some people try to convince you that you have to play by the rules. Listen, but whether you obey, you should decide yourself depending on your situation. The reality is "you SHOULD play by the rules" (not "you MUST play by the rules"). Just be aware that if you do not play by the rules, there might be consequences.
The situation not only applies in the situation of Runnable
, but with Java 8 also very frequently in the context of Streams and other places where functional interfaces have been introduced without the possibility to deal with checked exceptions. For example, Consumer
, Supplier
, Function
, BiFunction
and so on have all been declared without facilities to deal with checked exceptions.
So what are the situations and options?
In the below text, Runnable
is representative of any functional interface that doesn't declare exceptions, or declares exceptions too limited for the use case at hand.
- You've declared
Runnable
somewhere yourself, and could replace Runnable
with something else.
- Consider replacing
Runnable
with Callable<Void>
. Basically the same thing, but allowed to throw exceptions; and has to return null
in the end, which is a mild annoyance.
- Consider replacing
Runnable
with your own custom @FunctionalInterface
that can throw exactly those exceptions that you want.
- You've used an API, and alternatives are available. For example, some Java APIs are overloaded so you could use
Callable<Void>
instead of Runnable
.
- You've used an API, and there are no alternatives. In that case, you're still not out of options.
- You can wrap the exception in
RuntimeException
.
- You can hack the exception into a RuntimeException by using an unchecked cast.
You can try the following. It's a bit of a hack, but sometimes a hack is what we need. Because, whether an exception should be checked or unchecked is defined by its type, but practically should actually be defined by the situation.
@FunctionalInterface
public interface ThrowingRunnable extends Runnable {
@Override
default void run() {
try {
tryRun();
} catch (final Throwable t) {
throwUnchecked(t);
}
}
private static <E extends RuntimeException> void throwUnchecked(Throwable t) {
throw (E) t;
}
void tryRun() throws Throwable;
}
I prefer this over new RuntimeException(t)
because it has a shorter stack trace.
You can now do:
executorService.submit((ThrowingRunnable) () -> {throw new Exception()});
Disclaimer: The ability to perform unchecked casts in this way might actually be removed in future versions of Java, when generics type information is processed not only at compile time, but also at runtime.