File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why not unreachable statement??" Watch "why not unreachable statement??" New topic

why not unreachable statement??

air lulu
Ranch Hand

Joined: Jan 11, 2005
Posts: 42
try {

} catch (Exception e){
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) {

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

Joined: Feb 05, 2001
Posts: 17260

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.


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

Joined: Jul 08, 2003
Posts: 24189

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]
It is sorta covered in the JavaRanch Style Guide.
subject: why not unreachable statement??