I want to reproduce a part of InterruptedException behavior but I don't understand how it works...
So I have this code:
public static void main(String [] args){
try{
}catch(InterruptedException ie){
}
}
When I try to compile it I get this compiler error
Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body
I made a custom Exception which is not really an Exception because it doesn't extend Exception...
class MyException extends Throwable{
}
public static void main(String [] args){
try{
}catch(MyException ie){
}
}
Which shows the same compiler error
Unreachable catch block for MyException. This exception is never thrown from the try statement body
Then I did this
public static void main(String [] args){
try{
throw new MyException();
} catch(MyException e){
e.printStackTrace();
}
try{
throw new InterruptedException();
} catch(InterruptedException e){
e.printStackTrace();
}
}
And both of them compile fine.
But now comes the tricky part..
public static void main(String [] args){
try{
throw new MyException();
} catch(Exception e){
e.printStackTrace();
} catch(MyException e){
e.printStackTrace();
}
try{
throw new InterruptedException();
} catch(Exception e){
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
}
Compiler says
Unreachable catch block for InterruptedException. It is already handled by the catch block for Exception
Can you tell me how InterruptedException shows the "Unreachable catch block for InterruptedException. This exception is never thrown from the try statement body" compiler error and extends Exception in the same time, because when I extend exception my custom exceptions don't show this compiler error
As an example:
class MyException extends Exception{}
public static void main(String [] args){
try{
}catch(MyException me){
}
}
This code doesn't throw any compiler error
But the following code does
class MyException extends Throwable{}
public static void main(String [] args){
try{
}catch(MyException me){
}
}