Why does the compiler complain about InterruptedException when I try to use Threads sleep method?
Ans : The compiler is complaining, as it is required to do, that your code invokes a method that might throw a checked exception, but your code is not prepared to handle that exception; to stop the compiler from complaining, your method must either declare or catch the InterruptedException that Thread’s sleep method can throw. InterruptedException belongs to the set of checked exceptions a subset of exceptions that a Java compiler is required to track through any source code it handles. One of the compiler’s restrictions is that a method declare all the checked exceptions that it might throw. A method can throw an exception either by an explicit throw statement or by invoking another method that throws the exception. Thread’s sleep method declares that it can throw an InterruptedException; therefore, the compiler will examine any method that invokes sleep to check whether the exception is being dealt with. You have two options when writing a method that invokes sleep: • declare that yo