A method I am calling in run() in a class that implements Runnable) is designed to be throwing an exception.
But the Java compiler won\'t let me do that and suggests
If you want to pass a class that implements Runnable
into the Thread
framework, then you have to play by that framework's rules, see Ernest Friedman-Hill's answer why doing it otherwise is a bad idea.
I have a hunch, though, that you want to call run
method directly in your code, so your calling code can process the exception.
The answer to this problem is easy. Do not use Runnable
interface from Thread library, but instead create your own interface with the modified signature that allows checked exception to be thrown, e.g.
public interface MyRunnable
{
void myRun ( ) throws MyException;
}
You may even create an adapter that converts this interface to real Runnable
( by handling checked exception ) suitable for use in Thread framework.