• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

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: 4342
40
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.
 
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic