Posted By: JohnAskew | Jun 2nd @ 11:42 AM
page 1 of 1
Comments: 11 | Views: 580
JohnAskew
JohnAskew
9 girl in pink sweater

Is it the try block that incurs the overhead or perhaps the catch block, such that try/finally has little overhead?

danke

+1

Some time go I read that code inside the Try block is no subject to the same optimizations than regular code, But no so sure.

Hope that one of the new CLR 4.0 video series can shed some ligth on this topic.  What do you think C??

vesuvius
vesuvius
Das Glasperlenspiel

if you have a moment, try to start looking at code contacts forthcoming in .NET 4.0

In general it is the actual throwing of an exception that is a slow operation. That's one of the reasons why exceptions should not be used for control flow and why we have int.TryParse & co.

Overall I don't expact a simple try/catch/finally block to have a big impact on performance but:

- functions that contain try blocks are not inlined

- every line of code you add to a program will probably end up having some sort overhead. Bigger IL code, bigger metadata tables or some other executable data structures

Anyways, putting everything in a try/catch is likely bad practice. Sure, you can have some top level try/catch that catches and logs exceptions, but you don't go and put try/catch everywhere in the code.

It's the Catch part and all the necessary stack unwinding that really impacts performance. The Finally section might take a tiny little hit but it's almost too small to worth worrying about. If you have a situation that's going to call for a finally, then you should go right ahead and use one.

Minh
Minh
WOOH! WOOH!

Watch out there's a bug with try-catch-finally if you're using VS2003.

It is the Catch portion (i.e. when exceptions are thrown) that cause the performance impact... which is why you shouldn't rely on the Catch for handling normal run-time logic (which I have seen in the past).

I thought we agreed that was a bug in the C# compiler?

page 1 of 1
Comments: 11 | Views: 580
Microsoft Communities