Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy are try blocks expensive?
    text
    copied!<p>I've heard the advice that you should avoid try catch blocks if possible since they're expensive.</p> <p>My question is specifically about the .NET platform: Why are try blocks expensive?</p> <p><strong>Summary of Responses:</strong> </p> <p>There are clearly two camps on this issue: those that say that try blocks are expensive, and those that say "maybe a tiny little bit".</p> <p>Those that say try blocks are expensive normally mention the "high cost" of unwinding the call stack. Personally, I'm not convinced by that argument - specially after reading about how exceptions handlers are stored <a href="http://www.yoda.arachsys.com/csharp/exceptions2.html" rel="noreferrer">here</a>.</p> <p>Jon Skeet sits on the "maybe a tiny little bit" camp, and has written two articles on exceptions and performance which you can find <a href="http://www.yoda.arachsys.com/csharp/exceptions2.html" rel="noreferrer">here</a>.</p> <p>There was one article that I found extremely interesting: it talked about "other" performance implications of try blocks (not necessarily memory or cpu consumption). Peter Ritchie mentions that he found that code inside try blocks is not optimized as it'd otherwise be by the compiler. You can read about his findings <a href="http://msmvps.com/blogs/peterritchie/archive/2007/06/22/performance-implications-of-try-catch-finally.aspx" rel="noreferrer">here</a>.</p> <p>Finally, there's a blog entry about the issue from the man that implemented exceptions in the CLR. Go take a look at Chris Brumme's article <a href="http://blogs.msdn.com/cbrumme/archive/2003/10/01/51524.aspx" rel="noreferrer">here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload