File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript 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: 19672

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: 11246

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: 38424
N.Senthil Kumar wrote: . . . Will Catch Exception.
So you haven't taken any notice of the replies?
I agree. Here's the link:
subject: Doubt in Exception Handling.