CSharpFeeds - All your C# feeds in one place.

Sponsors

Sunday, October 01, 2006

A question of performance on Exceptions

by via Bill Wagner: C# Development Blog | MySmartChannels on 10/1/2006 9:08:21 PM

which I deftly don't answer

I received the following question in email recently:

While working on a problem report related to making sure that all our database updates are enclosed in try...catches.  Within the try we throw exceptions if anything goes wrong.  The catch clause contains all of the cleanup code.  Along the way, a question came to mind.  What kind of overhead is there in throwing an exception or is there any?  Your first book does not address that subject.

The answer is not so simple.  I discussed my general principles on exceptions here recently (http://www.srtsolutions.com/public/item/140872)  To recap, if a routine cannot do what it is supposed to do, it should throw an exception. That doesn’t necessarily lead to throw … all through your code, because a lot of code simple calls other methods (that might fail). 
For performance queries, I’ll defer to Rico Mariani. He covered this very question here:  http://blogs.msdn.com/ricom/archive/2003/12/19/44697.aspx . There are a couple points that are good to remember.  First, even Rico points out that every project is different.  Without proper measurements, you just don’t know if exceptions are a cost or not. 
Second, the static cost of exception handling code is minimal.  Which leads me to the final point: the measurement and the cost will be a function of how often the exception is thrown (which reflects how often the exceptional event happens). 
That does lead to some good design guidelines that you should always follow:  Rico points out that it would be good to idea to limit exceptions to cases that have a less than 1 in 1000 probability (Chance of failure is less than 0.1%).  Then, the runtime cost of exceptions (often, please measure your circumstances) won’t have that much of an effect.

email it!bookmark it!digg it!

Original Post: A question of performance on Exceptions

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us