Why cannot run() of Runnable throw checked Exceptions?

Inquisitive picture Inquisitive · Jul 10, 2012 · Viewed 17.4k times · Source

According to section 6.3.2 of JCIP :

Runnable is a fairly limiting abstraction; run can not return a value or throw checked exception .

run() can not return a value since its return type is void but why can not it throw a checked exception ?

Answer

Peter Lawrey picture Peter Lawrey · Jul 10, 2012

It cannot throw a checked exception because it wasn't declared as throwing a checked exception from the first version and it is too dangerous to change it.

Originally Runnable was only used in a wrapped Thread, and it was assumed the developer would want to catch all checked exceptions and handle them rather than logging them to System.err.

Callable was added when you can add individual tasks to an Executor where you can capture the result in a Future and any exception thrown.

Callable now allows you to return a value and optional declare a checked exception.

BTW: One way you can say you don't want a return or throw a checked exception from a callable is to use something like

Callable<Void> callable = new Callable<Void>() {
    public Void call() {
        // do something
        return null;
    }
};