0

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){     
    }
}
Mike
  • 279
  • 2
  • 11
  • The error message says it all: "It is already handled by the catch block for Exception". By definition, something that catched every exception catches InterruptedException. – JB Nizet Nov 09 '15 at 19:43
  • I did a mistake in the end of the question. I was wondering how InterruptedException extends Exception and it shows a compiler error when it is not thrown in the try body, because when I extend exception my custom exceptions don't show this compiler error – Mike Nov 09 '15 at 19:45
  • Edit your question and mae it clear. I don't understand what you mean. – JB Nizet Nov 09 '15 at 19:48
  • So if I design a class in which I extend the Exception class to make it a custom exception, I can't make it to show a compiler error when it is not thrown in the try block. class MyException extends Exception{} public static void main(String [] args){ try{ }catch(MyException me){ } }, doesn't show any compiler error – Mike Nov 09 '15 at 19:52
  • Having `MyException` extend `Exception` (or `Throwable`), plus having an empty `try` block while catching `MyException` is a compiler error for me. – rgettman Nov 09 '15 at 19:59

2 Answers2

1

This occurs because InterrupedException is a subclass of Exception, but Exception is already caught by the preceding catch block.

Section 11.2.3 of the JLS states:

It is a compile-time error if a catch clause can catch an exception class E1 and a preceding catch clause of the immediately enclosing try statement can catch E1 or a superclass of E1.

The block in the InterruptedException catch block would be unreachable code, which would be the justification for this compiler error.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • I did a mistake in the end of the question. I was wondering how InterruptedException extends Exception and it shows a compiler error when it is not thrown in the try body, because when I extend exception my custom exceptions don't show this compiler error – Mike Nov 09 '15 at 19:45
  • If `MyException` isn't a subclass of `Exception`, then there is no problem. – rgettman Nov 09 '15 at 19:50
  • I've just edited my question. It seems that I can't explain what is my problem... :) I know that my class doesn't extend the Exception class. But if I extend the Exception class I can't get a compiler error if my try block is empty – Mike Nov 09 '15 at 19:59
0

You are chatching throwable exception so it has to be thrown from somewhere

by the way : my compiler shows error for 1st script and for 2nd script

Angen
  • 400
  • 2
  • 10
  • I did a mistake in the end of the question. I was wondering how InterruptedException extends Exception and it shows a compiler error when it is not thrown in the try body, because when I extend exception my custom exceptions don't show this compiler error – Mike Nov 09 '15 at 19:45