• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why not unreachable statement??

 
air lulu
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try {

} catch (Exception e){
System.out.println("b");
}
-------------------------------------------------
if change "Exception e" to "IOException e",
compiler will report unreachable statement,why?
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because IOException is a checked exception,
try having something like this
try {
} catch(RuntimeException e) {
}

or
try {
} catch(NullPointerException e) {
}
These dont give the compiler error.

All subclasses of RuntimeException will not give any error because they are not checked exceptions
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to Animesh's answer.

Because IOException is a checked exception, the code within the try must call a method that could throw an IOException.

Since your code in the try is empty, the compiler knows that there is no chance in heck that it will ever throw an IOException and therefore any code in the catch is unreachable.

Now, catching an unchecked exception, means that the compiler won't know whether that type of exception will be thrown or not.

Hope that helps.

Mark
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We've actually been having an interesting discussion of this precise issue here. I think I agree with Jim that this is a compiler bug, at least with regard to standards compliance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic