• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception handling : catching "Exception"  RSS feed

 
Sreenath Madasu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
What is the logic behind catching specific Exception like "SQLException" Vs catching "Exception" as a whole?

Thanks
Sreenath
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point of catching checked exceptions is so your code can respond to a specific failure rather that a much less helpful "general error" type exception. An app might throw a SQLException if it was passed invalid SQL, or an IOException if it can't find a file etc.

(By the way, this would be better placed in the Java In General (beginner) forum)
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without getting into the checked vs unchecked discussion, let's just talk about checked. Something that I see often is:

There is no point at all in distinguishing the three catches if you just duplicate the response. So, catch different types only if you're really going to do something different. If they all do the same thing, just catch Exception.

Next, should you throw differnt types or just a common MyApplicationException? It's hard to guess if a future client will want to distinguish the various types of things that can go wrong in your method, but it's poor information hiding to reveal all the things that can go wrong. It's up to you.

Catching or throwing Exception of course invites that nasty unchecked discussion again. Search the various forums for more on that debate.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should not catch Exception. I won't say "ever" but in general avoid that like the plague. Wait for the compiler to complain about exceptions not being handled. Then add a catch if and only if you can actually do something about it. Otherwise declare that you throw it. In both cases, use the specific type complained about, not a general form.

Repeat that process for Runtime exceptions that you think you can correct, including the throws declaration if you can't. Declaring what you throw is good practice.

Now, if you want to log exceptions, that's cool, but rethrow the exception if it's not one you can handle. Maybe you want to chain it and throw a more specific type, that's OK too. You can do that whether it's checked or not.
[ August 26, 2005: Message edited by: Rick O'Shay ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sreenath Madasu:
Hi all!
What is the logic behind catching specific Exception like "SQLException" Vs catching "Exception" as a whole?

Thanks
Sreenath


That would be because you have something specific you want to do in response to a specific exception like SQLException as opposed to doing the same thing for every exception.
 
Rajesh Agarwal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For an SQLException we know that there is some Database related error, so we can give an appropriate error to the user. In case of FileNotFoundException we know that we are looking for some file which is not there and an appropriate message or action can be taken, all this as against the generic Exception
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"an indian",
please see the warning on your display name here.

If you do not correct your display name, your account will be deleted.

thanks,
Dave.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!