• Post Reply Bookmark Topic Watch Topic
  • New Topic

try and catch  RSS feed

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does having 4 to 5 try/catch blocks in a method slows down the execution of the method ?
 
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, unless the exceptions are thrown in the common cases.

The real question I have is why would you ever have a function with 5 try/catch blocks? Are they the same exception? If so, why not group them as one? If they are different they should probably be in separate functions, lest a single function is one long set of try/catch blocks. I could understand one try with 5 different catches, thats perfectly valid.

Also, it is a general practice not to catch Runtime exceptions unless your on the top-most level of a delegate. Finally, if you can avoid throwing exceptions in the first place (for example checking for an object equal to null rather than catching a null exception) it is always preferable to do so since there will be a performance hit if the exception is thrown versus avoiding the exception.
[ November 08, 2005: Message edited by: Scott Selikoff ]
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Throwing exceptions is expensive. But try/catch blocks themselves are essentially free when no exceptions are thrown.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!