This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 8 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: 19557

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

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: 36599
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.
Similar Threads
Unreachable Code
problrm with throw ing a exception but catching into a method declaring thorws
Exception doubt
error thrown in a constructor..