Try / catch block performance

Possible duplicate:
Cost of Execution & "try & rsquo;

I am told that adding a catch try block adds more performance in the order of 1000 times slower than without, in the example of a for loop of a million. It's true?

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

+6
source share
7 answers

From MSDN :

Search and design exception-heavy code can lead to a decent first-win victory. Keep in mind that this has nothing to do with try / catch blocks: you only incur expenses when the actual exception. You can use as many try / catch blocks as you want. Using exceptions is free of charge where you lose sight. For example, you should avoid things like using exceptions for control flow.

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

+27
source share

I could have sworn there was such a question just a few days ago, but I cannot find it ...

Just adding a try / catch block is unlikely to noticeably change performance if exceptions are not thrown, although this may prevent the method from being inlined. (There are different nesting rules in different versions of the CLR; I cannot remember the details.)

The real expense is when the exception is actually thrown - and even that expense is usually inflated. If you use exceptions appropriately (that is, only in truly exceptional or unforeseen situations with errors), then they are unlikely to be a significant performance hit, unless your service is too started to be considered "running" in any case.

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

I write many more try / finally blocks (in fact - almost always using using statements) than try / catch blocks. Try / catch sometimes comes up at the top level of the stack, so the service can continue to process the next request, even if it fails, but otherwise I rarely get exceptions. Sometimes itโ€™s worth grabbing one exception to wrap it in another exception โ€” basically translating the exception rather than handling it.

+8
source share

You should definitely check for such requirements (simple enough), but no, it wonโ€™t hurt you (it will cost, but not 1000 times).

Throwing exceptions and handling them is expensive. Having a try..catch..finally is not bad.

Now, having said this: if you are going to catch an exception, you need to make a plan of what you are going to do with it. There is no point in catching if you are just going to reverse, and many times, you cannot do this if you get an exception.

+2
source share

Adding catch catch blocks helps control your application due to exceptions that you do not control. The execution cost arises from the fact that you have chosen an exception if there are other alternatives. For example, throwing an exception to free the routine instead of simply returning from the subroutine causes a significant amount of overhead, which can be completely unnecessary.

+1
source share

I am told that adding an attempt to a catch block adds a lot of performance costing 1000 times slower than without, in example a for a cycle of a million. It's true?

Using try catch adds execution cost, but it is not a lot of performance.

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

No, it's better to use a catch try block when it makes sense.

0
source share

Why guess the cost of productivity when you can compare and see if it matters?

0
source share

It is true that exceptions are a very expensive operation. Also try..catch blocks the clutter of the code and makes it difficult to read. This means that exceptions are errors that can cause the application to crash most of the time.

I always work on the gap on all exceptions, so as soon as an error occurs, it throws, and I can pinpoint it. If everyone throws exceptions all the time, I get poor performance, and I can't use the break in all exceptions, which upsets me.

IMO does not use an exception for regular program events (for example, the types of users in a non-number and you are trying to parse it as a number). Use the usual program flow constructors (i.e. if) for this.

If you use functions that can throw, you make a choice. Is a fatal error => application crash. Is the error uncritical and probable => catch it (and possibly register it).

0
source share

All Articles