• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exception Handling  RSS feed

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to basics...

I am writing a file transfer code which needs try-catch block; having 5-6 catch blocks (with AuthEx, AuthrEx, ConnectionEx, IOEx etc.) for 5-6 lines of code in try block.

I am not sure why should I use 6 catch blocks for 5-6 lines of code instead of having single catch block for Exception.

Please add your comments.

Thanks.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say that the idea is to be able to tell which exception was thrown, and exactly where. Also, I think it may be a bit faster. I'm not exactly sure how you have this set up though...

Do you mean:



Or...


If you have the latter case, it may be better to just catch Exception... but I don't think you should have nested try/catch blocks.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he means something like the following:



You would need each of them to know exactly which exception is being thrown. However, if you don't care what is being thrown you can just catch them with Exception, although I wouldn't recommend it. I'm an information hog though, I like to get as much detailed information from things like that as I can.
 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for replies, I meant,



Even if I catch Exception instead of all (ExceptionA, ExceptionB, ExceptionC);
don't we get same printstack?

Please reply.
 
Wesley May
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll get the same stack, but you won't be able to (easily) have different code for handling different exceptions. It depends on what you want. If you only care whether or not an error occurred, and not what kind, then just catch Exception. If you want to handle each exception differently, then use multiple catches.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepika Joshi wrote:Hi,

Thanks for replies, I meant,



Even if I catch Exception instead of all (ExceptionA, ExceptionB, ExceptionC);
don't we get same printstack?

Please reply.


I believe so, but you won't know which exception actually happened. That could presesnt a problem when debugging an issue, but I can't say for sure without knowing what your program is/how it is coded.
 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wesley May wrote:You'll get the same stack, but you won't be able to (easily) have different code for handling different exceptions.


W. Joe Smith, Wesley May
Thanks a lot for replies.... I am not entertaining the exceptions & passing on, so I was not sure why I need multiple catch blocks; I will keep the same, which looks me logical & good in case of enhancements.

Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!