*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Exception Handling." Watch "Doubt in Exception Handling." New topic
Author

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.

Thanks


N.Senthil Kumar
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11175
    
  16

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
thanks.

Will Catch Exception.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
N.Senthil Kumar wrote: . . . Will Catch Exception.
So you haven't taken any notice of the replies?
 
Don't get me started about those stupid light bulbs.
 
subject: Doubt in Exception Handling.
 
Similar Threads
Exception doubt
Unreachable Code
error thrown in a constructor..
Exception
problrm with throw ing a exception but catching into a method declaring thorws