0

Following is my code, i am getting compile time error "Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body" which is fine

but when i am replacing InterruptedException with Exception then it doesn't gives any error, why so that too when both the exceptions are checked exceptions

public static void good() 
{
    try
    {
        eatCarrot();
    }
    catch(InterruptedException e)
    {

    }

}


private static void eatCarrot() 
{

}
  • It is because the exception which you want to catch is not thrown from the `try` clause. Change it to `exception e`, that should work fine. – Procrastinator Sep 11 '17 at 07:22
  • @procrastinator - i am aware of that, my question if i change InterruptedException to Exception then why does it not gives error, both the exceptions are checked exceptions? What is the reason of difference in behaviour. – springcloudlearner Sep 11 '17 at 07:24
  • InterruptedExceptions are for threads. You can not put them with the method calls. Check [here](https://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.html) for more details. – Procrastinator Sep 11 '17 at 07:26
  • @procrastinator but if i declare eatCarrot method as below then it works fine:- private static void eatCarrot() throws InterruptedException { } – springcloudlearner Sep 11 '17 at 07:28

0 Answers0