In Java thread, the \'run\' method cannot throw a \'checked exception\'. I came across this in the Core Java (vol 1) book. Can someone please explain the reasoning behind i
throws
declarations are part of the methods signature. To allow checked exceptions for Runnable#run
, one had to declare them on the Runnable
interface and had to try/catch
everytime we start a thread.
Then again, we usually don't call the run
method, we just implement it. We start()
a Thread and then, somehow, the run
method is called.
But the most obvious reason: When we start threads, we usually don't want to wait until the run
method terminates just to catch exceptions like this:
try {
new Worker().start(); // now wait until run has finished
} catch (SomeThreadException oops) {
// handle checked exception
}