This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Eclipse Collections Categorically: Level up your programming game and have Donald Raab on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

try and catch

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does having 4 to 5 try/catch blocks in a method slows down the execution of the method ?
 
author
Posts: 4356
45
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Throwing exceptions is expensive. But try/catch blocks themselves are essentially free when no exceptions are thrown.
 
I think she's lovely. It's this tiny ad that called her crazy:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic