<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blog.vuscode.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>Gotcha - Improper exception handling</title><link>http://blog.vuscode.com/malovicn/archive/2007/04/01/gotcha-improper-exception-handling.aspx</link><description>I was reading today a document written by Juval Lowy from IDesign &amp;quot;C# Coding standard - Guidelines and Best Practices V2.2&amp;quot; and under No 17 rule in Coding Practices I found next thing: 17. In a catch statement that throws an exception, always</description><dc:language>en</dc:language><generator>CommunityServer 2007 (Build: 20416.853)</generator><item><title>Viagra.</title><link>http://blog.vuscode.com/malovicn/archive/2007/04/01/gotcha-improper-exception-handling.aspx#615</link><pubDate>Sun, 29 Mar 2009 11:54:00 GMT</pubDate><guid isPermaLink="false">4fad33ed-6fa7-4667-b447-1fa3be86b49e:615</guid><dc:creator>Viagra.</dc:creator><description>&lt;p&gt;Viagra. Generic viagra mastercard. Order viagra online. Buy cheap viagra online uk. Free viagra.&lt;/p&gt;
&lt;img src="http://blog.vuscode.com/aggbug.aspx?PostID=615" width="1" height="1"&gt;</description></item><item><title>re: Gotcha - Improper exception handling</title><link>http://blog.vuscode.com/malovicn/archive/2007/04/01/gotcha-improper-exception-handling.aspx#274</link><pubDate>Tue, 04 Dec 2007 14:05:32 GMT</pubDate><guid isPermaLink="false">4fad33ed-6fa7-4667-b447-1fa3be86b49e:274</guid><dc:creator>AJ.NET</dc:creator><description>FYI:
System.Exception has a property &amp;quot;Data&amp;quot; (type IDictionary) (new in .NET 2.0). Thus it is no longer necessary to provide a derived exception class &amp;quot;only&amp;quot; to add some piece of information.

For me the reasons to wrap an exception are usually a) to provide a better suited exception class to _describe_ the error or b) to provide better destinction of errors for code that _handles_ the error (i.e. catch-handlers). Providing the inner exception is of course mandatory...
&lt;img src="http://blog.vuscode.com/aggbug.aspx?PostID=274" width="1" height="1"&gt;</description></item><item><title>No soup for me  &amp;raquo; Blog Archive   &amp;raquo; Makes me wanna throw ex;</title><link>http://blog.vuscode.com/malovicn/archive/2007/04/01/gotcha-improper-exception-handling.aspx#257</link><pubDate>Fri, 30 Nov 2007 23:17:17 GMT</pubDate><guid isPermaLink="false">4fad33ed-6fa7-4667-b447-1fa3be86b49e:257</guid><dc:creator>No soup for me  » Blog Archive   » Makes me wanna throw ex;</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;No soup for me &amp;nbsp;&amp;amp;raquo; Blog Archive &amp;nbsp; &amp;amp;raquo; Makes me wanna throw ex;&lt;/p&gt;
&lt;img src="http://blog.vuscode.com/aggbug.aspx?PostID=257" width="1" height="1"&gt;</description></item><item><title>re: Gotcha - Improper exception handling</title><link>http://blog.vuscode.com/malovicn/archive/2007/04/01/gotcha-improper-exception-handling.aspx#256</link><pubDate>Fri, 30 Nov 2007 20:55:24 GMT</pubDate><guid isPermaLink="false">4fad33ed-6fa7-4667-b447-1fa3be86b49e:256</guid><dc:creator>Nikola Malovic</dc:creator><description>&lt;p&gt;Justin,&lt;/p&gt;
&lt;p&gt;I agree 100% that raising the Exception type of exception is totally wrong practice but IMHO using of ApplicationException is equally bad and I am sure I read it on couple of places not recomended any more from MS. &lt;/p&gt;
&lt;p&gt;What we should use is either some specific exception which match the exception case scenario such are InvalidOperationException, ArgumentException etc or our own custom exceptions.&lt;/p&gt;
&lt;p&gt;I am really glad that FXCop reports an issue with usage of Exception, which is very useful in answering the &amp;quot;why not just use Exception&amp;quot; :)&lt;/p&gt;
&lt;p&gt;mcgurk,&lt;/p&gt;
&lt;p&gt;I agree it is funny and small thing but I spent an descent amount of time hunting bugs in cases where the stack was destroyed or in even worse swallowing catch {} case (which btw makes me bloodthirsty :) ) &lt;/p&gt;
&lt;img src="http://blog.vuscode.com/aggbug.aspx?PostID=256" width="1" height="1"&gt;</description></item><item><title>re: Gotcha - Improper exception handling</title><link>http://blog.vuscode.com/malovicn/archive/2007/04/01/gotcha-improper-exception-handling.aspx#255</link><pubDate>Fri, 30 Nov 2007 18:47:14 GMT</pubDate><guid isPermaLink="false">4fad33ed-6fa7-4667-b447-1fa3be86b49e:255</guid><dc:creator>mcgurk</dc:creator><description>I always laugh when I see people discussing best practices and then do something like throw a System.Exception object.  

However, I agree with you.  If you must catch and throw, wrap a base exception in another exception type and throw that.  Primarily because people up the call chain won&amp;#39;t have to attempt to catch twenty thousand different types of exceptions.&lt;img src="http://blog.vuscode.com/aggbug.aspx?PostID=255" width="1" height="1"&gt;</description></item><item><title>re: Gotcha - Improper exception handling</title><link>http://blog.vuscode.com/malovicn/archive/2007/04/01/gotcha-improper-exception-handling.aspx#254</link><pubDate>Fri, 30 Nov 2007 18:41:19 GMT</pubDate><guid isPermaLink="false">4fad33ed-6fa7-4667-b447-1fa3be86b49e:254</guid><dc:creator>Justin Etheredge</dc:creator><description>I think that &amp;quot;throw;&amp;quot; is your best option if you don&amp;#39;t have any additional data to add to your exception. If you do have data, then passing the old exception as an inner exception is the best way to go. They are both &amp;quot;best practices&amp;quot; for their respective situations. Which is why I hate the term &amp;quot;best practice&amp;quot;, it makes it sound way too much like a universal solution. But I digress.

Also, you should point out that raising the type &amp;quot;Exception&amp;quot; is not best practice and it should always be subclassed, and preferably from the ApplicationException class if you are really being anal about it. :-)&lt;img src="http://blog.vuscode.com/aggbug.aspx?PostID=254" width="1" height="1"&gt;</description></item><item><title>re: Gotcha - Improper exception handling</title><link>http://blog.vuscode.com/malovicn/archive/2007/04/01/gotcha-improper-exception-handling.aspx#252</link><pubDate>Fri, 30 Nov 2007 16:48:10 GMT</pubDate><guid isPermaLink="false">4fad33ed-6fa7-4667-b447-1fa3be86b49e:252</guid><dc:creator>Javier Romero</dc:creator><description>Nice explanation, there are many devs that don&amp;#39;t know the real and useful information of the stack... 
&lt;img src="http://blog.vuscode.com/aggbug.aspx?PostID=252" width="1" height="1"&gt;</description></item><item><title>Why throw; is not allways  the best way to rethrow exceptions</title><link>http://blog.vuscode.com/malovicn/archive/2007/04/01/gotcha-improper-exception-handling.aspx#250</link><pubDate>Fri, 30 Nov 2007 12:39:36 GMT</pubDate><guid isPermaLink="false">4fad33ed-6fa7-4667-b447-1fa3be86b49e:250</guid><dc:creator>DotNetKicks.com</dc:creator><description>&lt;p&gt;You've been kicked (a good thing) - Trackback from DotNetKicks.com&lt;/p&gt;
&lt;img src="http://blog.vuscode.com/aggbug.aspx?PostID=250" width="1" height="1"&gt;</description></item></channel></rss>