• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exception confusion

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi freinds i have the following query.......
heres the code...this code is given in scjp guide by kath sierra and berth bates pageno. 370

it says that the following code will show the following compiler error
try{

//do risky IO things

}catch (IOException e) {

// handle genral IOExceptions

}catch (FileNotFoundException ex) {

// handle just FileNotFoundException
}

compiler error :

TextEX.java:: exception java.io.FileNotFoundException has already been caught

} catch (FileNotFoundException ex ) {


my confusion is that FileNotFoundException is a subcllass of IOException so we know that an exception superclass can catch the exception of subclaas so here if its able to catch the exception of subclass then why will be there any error
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Amit,

The above code is giving you the error because its the rule that exception hierarchy should follow from the
most specific exception to the most general exception. There is a great example to understand this concept
in K&B which is basically depicted by giving a story of NET BALL thrown from the top of the story building, so
the above program is giving an exception because you have specific the most general exception first then the
sub-class which violates the rule. In such a scenario always remember the rule of thumb

Most Specific Exception -> Most General Exception
FileNotFoundException -> IOException.

Hope this helps,
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amit Dhar wrote:


Here FileNotFoundException will also be caught by the first catch block and the program conrol would never reach the second catch block at all. It is normal for a Java compiler to raise an error if it finds some unreachable code.
If you swap both the catch blocks, it would be fine. The rule is - in case you want to catch both XXexception & superXXexception, then you will have to catch the subclass exception first followed by the other, for not ending up with some unreachable code.

Amit, please use code blocks to differentiate your code, so people may understand better. Otherwise everything might look as some plain text altogether.
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit Dhar ..Welcome to JavaRanch.
Please use code tags while posting your code.
See the heriarchy of Exception.you will find the Throwable to be at the TOP..
and the reason why you got the exception in your code is..
because

You have already catched al the exception by using

So,FileNotFoundException will be unreachble for the comppiler
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic