This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Doubt in Exception Handling. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Exception Handling." Watch "Doubt in Exception Handling." New topic

Doubt in Exception Handling.

N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50

Say for example,

i am closing a InputStream Object, the Compiler says Handle IOException.

May I know which is best and why..
whether to catch Exception or IOException .

That is Specific Exception or General Base Exception.


N.Senthil Kumar
Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

If you know exactly which exceptions can be thrown, try to catch the specific ones as much as possible. If you catch Exception you may catch a NullPointerException or IllegalArgumentException you weren't expecting.

How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11152

you may also catch an exception that is (better) handled elsewhere.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50

Will Catch Exception.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37913
N.Senthil Kumar wrote: . . . Will Catch Exception.
So you haven't taken any notice of the replies?
Consider Paul's rocket mass heater.
subject: Doubt in Exception Handling.
Similar Threads
Exception doubt
Unreachable Code
error thrown in a constructor..
problrm with throw ing a exception but catching into a method declaring thorws