aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why not unreachable statement?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why not unreachable statement??" Watch "why not unreachable statement??" New topic
Author

why not unreachable statement??

air lulu
Ranch Hand

Joined: Jan 11, 2005
Posts: 42
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

Joined: Jul 19, 2004
Posts: 298
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

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why not unreachable statement??