There are two kinds of exceptions in Java: checked and unchecked exceptions.
For checked exceptions, the compiler checks if your program handles them, either by catching them or by specifying (with a throws
clause) that the method in which the exception might happen, that the method might throw that kind of exception.
Exception classes that are subclasses of java.lang.RuntimeException
(and RuntimeException
itself) are unchecked exceptions. For those exceptions, the compiler doesn't do the check - so you are not required to catch them or to specify that you might throw them.
Class InterruptedException
is a checked exception, so you must either catch it or declare that your method might throw it. You are throwing the exception from the catch
block, so you must specify that your method might throw it:
public void invalid() throws InterruptedException {
// ...
Exception classes that extend java.lang.Exception
(except RuntimeException
and subclasses) are checked exceptions.
See Sun's Java Tutorial about exceptions for detailed information.