Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Exception handling : catching "Exception"

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic