<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The True Cost of .NET Exceptions -- Solution</title><link>http://blogs.msdn.com/ricom/archive/2006/09/25/the-true-cost-of-net-exceptions-solution.aspx</link><description>Well, once again my elite readers have made a solution posting by me nearly redundant. There were many good viewpoints in response to my article including some especially excellent comments that were spot on. Now there are several things worth saying,</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Understanding the true cost of throwing an exception</title><link>http://blogs.msdn.com/ricom/archive/2006/09/25/the-true-cost-of-net-exceptions-solution.aspx#772346</link><pubDate>Tue, 26 Sep 2006 18:22:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:772346</guid><dc:creator>Jeff Stong</dc:creator><description>Wondering about the performance costs of throwing and catching exceptions in your application?&amp;amp;amp;nbsp;...</description></item><item><title>re: The True Cost of .NET Exceptions -- Solution</title><link>http://blogs.msdn.com/ricom/archive/2006/09/25/the-true-cost-of-net-exceptions-solution.aspx#772567</link><pubDate>Tue, 26 Sep 2006 20:58:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:772567</guid><dc:creator>nativecpp</dc:creator><description>I will think that unmanaged code also follows the same rules (page fault, etc). If so, why would that be such a big topic/argument in .NET and not in unmanaged ?? I don't recall people talking much about the cost of exception in unmanaged code as we don't throw needlessly.&lt;br&gt;&lt;br&gt;I think it is important for .NET programmers (myself included) to know that just because a feature is easily avail does NOT mean it should be used freely.&lt;br&gt;&lt;br&gt;I have been working with .NET/C# for a couple of years and see people abusing the power of C#/.NET.&lt;br&gt;&lt;br&gt;Rico, keep the good topics coming. I love reading your blog which to me is thought-provoking.&lt;br&gt;&lt;br&gt;Thanks</description></item><item><title>re: The True Cost of .NET Exceptions -- Solution</title><link>http://blogs.msdn.com/ricom/archive/2006/09/25/the-true-cost-of-net-exceptions-solution.aspx#773198</link><pubDate>Wed, 27 Sep 2006 06:51:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:773198</guid><dc:creator>Al Tenhundfeld</dc:creator><description>Hi Rico, I just discovered your blog recently, and I must say that I'm impressed by the quality of your posts and your readers. &lt;br&gt;&lt;br&gt;I was reading back through some old posts on exceptions, and I came across this &amp;quot;almost rule&amp;quot;:...but on the other hand throwing an exception due to invalid user input, or badly formatted text from an external system, could be a bad idea.&lt;br&gt;&lt;br&gt;I recognize that you emphasize &amp;quot;could,&amp;quot; but I'm curious what other strategies people use. &lt;br&gt;&lt;br&gt;[Long boring description of a previous project]&lt;br&gt;On a recent project, we decided to use exceptions in the facade layer to communicate business rules violations and invalid user input back to the web UI. We tried to validate as much as possible client-side in the web UI, but in the domain/facade layer we also validated all business rules and user input. Our strategy was to define a custom exception with property that exposed a collection of error messages. We would validate as much as possible before throwing the exception. &lt;br&gt;&lt;br&gt;For example, we would validate a data object, appending any violations to a single custom exception. When validation was finished, we would throw that one exception, which contained a collection of violation objects, up to the UI. It was actually slightly more sophisticated, using a generational approach based on dependencies, but the gist is correct. Would you say this approach sounds reasonable from my description? To me, this strategy just seemed to have the most intuitive design, and we took steps to minimize the number of exceptions being thrown. We also instrumented the facade pretty well; so we could see which violations occurred more often and improve client-side validation or tweak UI flow to reduce them.&lt;br&gt;[/boring description]&lt;br&gt;&lt;br&gt;We considered forcing all of our facade API methods to return error information, but that felt hacky to us and put the burden on our API consumers to inspect the returned objects. We were also developing a platform, not just a single app; so we wanted to be certain the UI developers would always know when something was rejected. We also briefly considered an approach involving callback delegates, but that seemed to be incompatible with our SOA goals. &lt;br&gt;&lt;br&gt;Anyhow, sorry if this is a little offtopic. I'm just at the point in my career where I'm transitioning from developer to architect, and I've never really found a satisfactory solution for this issue of communicating business rules/user input validations across layers/tiers.&lt;br&gt;&lt;br&gt;&lt;br&gt;Thanks.</description></item><item><title>re: The True Cost of .NET Exceptions -- Solution</title><link>http://blogs.msdn.com/ricom/archive/2006/09/25/the-true-cost-of-net-exceptions-solution.aspx#773203</link><pubDate>Wed, 27 Sep 2006 07:01:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:773203</guid><dc:creator>ricom</dc:creator><description>Hi Al, glad you're enjoying the postings. &amp;nbsp;Welcome to perf tidbits :)&lt;br&gt;&lt;br&gt;It's no surprise that you've never found a satisfactory solution to communicating errors across layers because I doubt there is one. &amp;nbsp;Well, I haven't found it and I've been looking for probably as long as you if not longer.&lt;br&gt;&lt;br&gt;What you did in the case above seems reasonable but there is no one universal answer. &amp;nbsp;Some factors:&lt;br&gt;&lt;br&gt;1) The volume of input and granularity of reporting (per field, per record, per batch, per disk drive :)&lt;br&gt;&lt;br&gt;2) The frequency of errors&lt;br&gt;&lt;br&gt;3) The time/latency requirements if any&lt;br&gt;&lt;br&gt;4) The amount of slack you otherwise have available (i.e. how perfect do you have to be)&lt;br&gt;&lt;br&gt;That's a short list, the real list is longer but those basic factors talk about some key things. &amp;nbsp;&lt;br&gt;&lt;br&gt;Do you design an API like Parse? or TryParse? &amp;nbsp;There's nothing &amp;quot;wrong&amp;quot; with Parse it's a fine API and plenty of times it's what you need. &amp;nbsp;On the other hand TryParse is indespensible when needed.&lt;br&gt;&lt;br&gt;I don't know a universal answer.</description></item><item><title>re: The True Cost of .NET Exceptions -- Solution</title><link>http://blogs.msdn.com/ricom/archive/2006/09/25/the-true-cost-of-net-exceptions-solution.aspx#808407</link><pubDate>Mon, 09 Oct 2006 18:57:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:808407</guid><dc:creator>Marcel Popescu</dc:creator><description>&lt;p&gt;Hmm... is the following conclusion correct, in that case?&lt;/p&gt;
&lt;p&gt;New rule: throwing ONE exception rarely is very costly; throwing a LOT of exception, often, is not a big deal.&lt;/p&gt;
</description></item><item><title>re: The True Cost of .NET Exceptions -- Solution</title><link>http://blogs.msdn.com/ricom/archive/2006/09/25/the-true-cost-of-net-exceptions-solution.aspx#816839</link><pubDate>Wed, 11 Oct 2006 15:44:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:816839</guid><dc:creator>ricom</dc:creator><description>&lt;p&gt;No, I don't think I can support that rule. &amp;nbsp;The logic in the sample for instance would have been probably 10000 times faster without the exception, even at the discount rate.&lt;/p&gt;
&lt;p&gt;You're talking about the difference between an operation that you can do say several O(10^4) per second versus branching which you can do say O(10^8) times per second.&lt;/p&gt;
&lt;p&gt;And if you're doing it often, then probably you really should be branching rather than throwing.&lt;/p&gt;
&lt;p&gt;Losing a factor -- conservatively -- of 10^4 is a big deal.&lt;/p&gt;
</description></item><item><title>Do Performance Analysis in Context</title><link>http://blogs.msdn.com/ricom/archive/2006/09/25/the-true-cost-of-net-exceptions-solution.aspx#1342002</link><pubDate>Thu, 21 Dec 2006 23:04:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1342002</guid><dc:creator>Rico Mariani's Performance Tidbits</dc:creator><description>&lt;p&gt;I had an interesting email conversation here at work recently and as usual I can't share it all but I&lt;/p&gt;
</description></item></channel></rss>