This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

try/catch  RSS feed

 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we are supposed to use the most appropriate exception in the catch clause.

for example

try{
FileInputStream fin = new FileInputStream("A.java");
}catch(FileNotFoundException exp1){
System.out.println("file not found");
}

Why it's NOT advisable to use something like

try{
FileInputStream fin = new FileInputStream("A.java");
}catch(Exception exp1){
System.out.println("file not found");
}

kindly explain.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the class Exception is much wider than FileNotFoundException, and will catch every subclass of Exception.

So unless you check to see what type the parameter in the catch is, you will be printing out file not found even if that is not the problem.
 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well the class Exception is much wider than FileNotFoundException, and will catch every subclass of Exception.


Agreed.


So unless you check to see what type the parameter in the catch is, you will be printing out file not found even if that is not the problem.


didn't get you here.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More than once, in code I've come across catch blocks where, for example, Exception was caught and it was assumed to be of a more specific type:

The trouble with this is, as they say, when you assume, you make an "ass" of "u" and "me". Some other exception could in fact be occurring, perhaps because of a programming error (a bug), and you're missing it or misinterpreting it. Another way this arises is that the code mutates over time: perhaps originally "file not found" was the only possible error (probablematic, but let's not argue), but over time, code is editted, say elsewhere in subroutines. Now different errors may arise and this is not being noticed or accounted for.

One reason people cast the net wide with an overly general catch block is because code may throw several different exceptions:

All I can say in this case is to be a mensch and do the right thing -- don't just catch Exception unless you really don't what to know or care what went wrong.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try getting details of the exceptions.
Try:-and then you will have a printout with the correct name of the exception in

CR
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!