9

does try/catch affects performance if no exception thrown in try block?
how about try/catch/finally?

  • 1
    possible duplicate of [Do try/catch blocks hurt performance when exceptions are not thrown?](http://stackoverflow.com/questions/1308432/do-try-catch-blocks-hurt-performance-when-exceptions-are-not-thrown) – Andrew Barber Dec 20 '10 at 17:53
  • @HPT what kind of question is this???. try/catch are a must for every method in-order to handle all kinds of exceptions. What is the use of a software product when it can't fail gracefully or recover from errors?? what has performance has to do when the basic itself is weak. – A_Var Dec 20 '10 at 17:53
  • possible duplicate of http://stackoverflow.com/questions/1771216/is-there-really-a-performance-hit-when-catching-exceptions – Simon Mourier Dec 20 '10 at 17:54
  • 1
    If I had a penny for every "does X affect performance"/"is X faster than Y" question... really, you people need a hobby. Or an introduction on where you can look for optimization oppoturnities and where not (and when you shouldn't even try). –  Dec 20 '10 at 17:54
  • @delnan - you'd be a rich person!! But your bank would *not* like you very much for all the pennies you'd be carting in to them. – Andrew Barber Dec 20 '10 at 17:57
  • 3
    @A_Var: "try/catch are a must for every method"... I disagree with that statement. A lot. There's no sense in catching an exception unless you actually plan to _handle_ it in some way. Not every method needs this. There's nothing wrong with exposing methods which can potentially throw exceptions, it should just be documented what they may throw and why. – David Dec 20 '10 at 18:00

4 Answers4

14

Neither try/catch nor try/catch/finally affects performance to any significant degree. Exceptions being created do affect performance, of course (and that is whether they are caught, or not)

Do try/catch blocks hurt performance when exceptions are not thrown?

Community
  • 1
  • 1
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
4

try/catch will only effect performance if an Exception is thrown (but that still isn't because of try/catch, it is because an Exception is being created).

try/catch/finally does not add any additional overhead over try/catch.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
2

Creating an exception does incur some performance cost. Generally, you want to catch an exception only when there's something you actually need to do with that exception at that point in the code.

Note that, for the purpose of closing resources or performing other necessary tasks after a potential failure, you can employ simply a try/finally block. The finally will still execute as it should, you're just not catching the exception in the process. Rather, you let it bubble up to wherever it needs to go.

David
  • 208,112
  • 36
  • 198
  • 279
  • There's nothing incorrect about your answer, but I wanted to make a small point; whether you catch an exception or not has no real effect on performance (aside from the obvious affect of the application crashing if it's not handled *anywhere* at all). I add that detail only because the OP seems to be concentrating a bit on whether *catching* the exception has some effect. As you note, it is the *creation* of an exception that is 'heavy', not handling/not. – Andrew Barber Dec 20 '10 at 17:54
  • @Andrew Barber: That's correct, ya. Your answer definitely worded that part better than mine. I was just focusing more on the point of not wanting to catch when one doesn't have to, which I suppose is tangential to the original question. I've seen plenty of code where an exception is caught for no other reason than to just throw again, or even instantiate a new (custom) exception and add the caught one to it. – David Dec 20 '10 at 17:58
  • I've seen code that does the latter, needlessly, inside an otherwise fairly tight loop that iterates a few hundred times while blocking the UI, where at least 10% of the iterations can be expected to result in an exception. Fun times! – Andrew Barber Dec 20 '10 at 18:01
0

No, a try/catch block does not incur any performance cost when an exception is not thrown.

Read a great MSDN article about this here: http://msdn.microsoft.com/en-us/library/ms973839.aspx

dontangg
  • 4,729
  • 1
  • 26
  • 38