Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in Exception Handling.  RSS feed

 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you may also catch an exception that is (better) handled elsewhere.
 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks.

Will Catch Exception.
 
Campbell Ritchie
Marshal
Posts: 55741
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N.Senthil Kumar wrote: . . . Will Catch Exception.
So you haven't taken any notice of the replies?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!