13

Possible Duplicate:
Performance Cost Of ‘try’

I am being told that adding a try catch block adds major performance cost in the order of 1000 times slower than without, in the example of a for loop of a million. Is this true?

Isn't it best to use try catch block as much as possible?

Community
  • 1
  • 1
GenEric35
  • 7,083
  • 3
  • 24
  • 25
  • http://stackoverflow.com/questions/164613/why-are-try-blocks-expensive http://stackoverflow.com/questions/2075151/try-catch-blocks-always-expensive-closed – Greg Aug 13 '10 at 19:36
  • 2
    Duplicate [many, many times over](http://stackoverflow.com/search?q=[c%23]+exception+performance). Search first, please. – Craig Stuntz Aug 13 '10 at 19:37
  • 2
    probably the *best* duplicate of them: http://stackoverflow.com/questions/1308432/do-try-catch-blocks-hurt-performance-when-exceptions-are-not-thrown – x4u Aug 13 '10 at 19:40
  • I have searched for cost of try catch block, didn't return anything relevant, it even offered similar topics and non were related – GenEric35 Aug 13 '10 at 19:48

7 Answers7

36

From MSDN site:

Finding and designing away exception-heavy code can result in a decent perf win. Bear in mind that this has nothing to do with try/catch blocks: you only incur the cost when the actual exception is thrown. You can use as many try/catch blocks as you want. Using exceptions gratuitously is where you lose performance. For example, you should stay away from things like using exceptions for control flow.

Also see these related SO questions: (1) (2) (3) and (4).

Community
  • 1
  • 1
Kevin Crowell
  • 10,082
  • 4
  • 35
  • 51
11

I could swear there was a question like this just a few days ago, but I can't find it...

Just adding a try/catch block is unlikely to change the performance noticeably when exceptions aren't being thrown, although it may prevent a method from being inlined. (Different CLR versions have different rules around inlining; I can't remember the details.)

The real expense is when an exception is actually thrown - and even that expense is usually overblown. If you use exceptions appropriately (i.e. only in genuinely exceptional or unexpected error situations) then they're unlikely to be a significant performance hit except in cases where your service is too hosed to be considered "working" anyway.

As for whether you should use try/catch blocks as much as possible - absolutely not! You should usually only catch an exception if you can actually handle it - which is relatively rare. In particular, just swallowing an exception is almost always the wrong thing to do.

I write far more try/finally blocks (effectively - almost always via using statements) than try/catch blocks. Try/catch is sometimes appropriate at the top level of a stack, so that a service can keep processing the next request even if one fails, but otherwise I rarely catch exceptions. Sometimes it's worth catching one exception in order to wrap it in a different exception - basically translating the exception rather than really handling it.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    .. relatively rare? So if you open a file which doesn't exist crash the app? If you get a 404 crash the app? If you convert a string without digits to a number crash the app? I would say that unless the program can't recover let it crash. But if you can recover shouldn't that be preferred? – Toad Aug 13 '10 at 19:43
  • 3
    @Toad: If you're converting a string to a number, you should use TryParse if it's user input, for one thing. But the point is that most of these should be caught - if at all - at a relatively high level. That's harder for client apps than server side apps, but note the question tags - the OP is talking from an ASP.NET perspective. Usually if there's an exception, the right approach is to abort the request and report an error to the user. How many catch blocks does that require, really? – Jon Skeet Aug 13 '10 at 19:59
3

You should definitely test claims like this (easy enough), but no, that isn't going to hurt you (it'll have a cost, but not 1000's of times).

Throwing exceptions and handling them is expensive. Having a try..catch..finally isn't bad.

Now with that said, If you are going to catch an exception, you need to have a plan for what you are going to do with it. There is no point in catching if you are just going to rethrow, and a lot of times, there's not much you can do if you get an exception.

JMarsch
  • 21,484
  • 15
  • 77
  • 125
1

Adding try catch blocks helps control your application from exceptions you have no control over. The performance cost comes from throwing an exception when there are other alternatives. For example throwing an exception to bail out of a routine instead of simply returning from a routine causes a significant amount of overhead, which may be completely unnecessary.

Russ
  • 4,091
  • 21
  • 32
0

I am being told that adding a try catch block adds major performance cost in the order of 1000 times slower than without, in the example of a for loop of a million. Is this true?

Using try catch adds performance cost, but it isn't a major performance cost.

Isn't it best to use try catch block as much as possible?

No, it is best to use try catch block when makes sense.

Daniel Moura
  • 7,816
  • 5
  • 35
  • 60
0

Why guess at the performance costs, when you can benchmark and see if it matters?

Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
0

It's true that exceptions a very expensive operation. Also try..catch blocks clutter the code and make it hard to read. That said exceptions are fine for errors that should crash the application most of the time.

I always run on break on all exceptions, so as soon as an error happens it throws and I can pinpoint it fairly easy. If everyone is throwing exceptions all the time I get bad performance and I can't use the break on all exceptions, that's makes me sad.

IMO don't use exception for normal program events (like user types in a non-number and you try to parse it as a number). Use the normal program flow constructs for that (ie if).

If you use functions that may throw you make a choice. Is the error critical => crash the app. Is the error non-critical and likely => catch it (and potentially log it).