<?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>Code Analysis Team Blog : Exceptions</title><link>http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx</link><description>Tags: Exceptions</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How to Design Exception Hierarchies</title><link>http://blogs.msdn.com/fxcop/archive/2007/02/01/how-to-design-exception-hierarchies.aspx</link><pubDate>Thu, 01 Feb 2007 19:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1561628</guid><dc:creator>David M. Kean</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1561628.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1561628</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1561628</wfw:comment><description>&lt;P&gt;Krzysztof Cwalina, owner of the &lt;A class="" href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756" mce_href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756"&gt;Framework Design Guidelines&lt;/A&gt;, has written a great post on &lt;A class="" href="http://blogs.msdn.com/kcwalina/archive/2007/01/30/ExceptionHierarchies.aspx" mce_href="http://blogs.msdn.com/kcwalina/archive/2007/01/30/ExceptionHierarchies.aspx"&gt;designing exception hierarchies&lt;/A&gt;. He gives a great overview on the different&amp;nbsp;categories of exceptions, which are placed under two main buckets he calls &lt;EM&gt;usage&lt;/EM&gt; and &lt;EM&gt;system&lt;/EM&gt;, and the right situations to throw them.&lt;/P&gt;
&lt;P&gt;This post also goes hand-in-hand with some of the previous posts on this blog:&lt;BR&gt;&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2007/01/22/faq-what-exception-should-i-throw-instead-of-the-reserved-exceptions-found-by-donotraisereservedexceptiontypes.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2007/01/22/faq-what-exception-should-i-throw-instead-of-the-reserved-exceptions-found-by-donotraisereservedexceptiontypes.aspx"&gt;FAQ: What exception should I throw instead of the reserved exceptions that DoNotRaiseReservedExceptionTypes warns against?&lt;/A&gt;&lt;BR&gt;FAQ: Why does FxCop warn against catch(Exception)? (&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx"&gt;Part 1&lt;/A&gt;, &lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx"&gt;Part 2&lt;/A&gt;&amp;nbsp;and &lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx"&gt;Part 3&lt;/A&gt;)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1561628" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/Framework+Design+Guidelines/default.aspx">Framework Design Guidelines</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/FxCop/default.aspx">FxCop</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx">Exceptions</category></item><item><title>FAQ: What exception should I throw instead of the reserved exceptions that DoNotRaiseReservedExceptionTypes warns against?</title><link>http://blogs.msdn.com/fxcop/archive/2007/01/22/faq-what-exception-should-i-throw-instead-of-the-reserved-exceptions-found-by-donotraisereservedexceptiontypes.aspx</link><pubDate>Mon, 22 Jan 2007 19:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1430357</guid><dc:creator>David M. Kean</dc:creator><slash:comments>14</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/1430357.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=1430357</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=1430357</wfw:comment><description>&lt;P&gt;Throwing a general exception type such as &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.exception.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.exception.aspx"&gt;System.Exception&lt;/A&gt; or &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.systemexception.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.systemexception.aspx"&gt;System.SystemException&lt;/A&gt; in a library or Framework forces&amp;nbsp;consumers to catch all exceptions, including unknown exceptions that they do not know how to handle (see &lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/14/faq-why-does-fxcop-warn-against-catch-exception-nick-guerrera.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/faq-why-does-fxcop-warn-against-catch-exception-nick-guerrera.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)?&lt;/A&gt;&amp;nbsp;for reasons as to why this is bad). &lt;/P&gt;
&lt;P&gt;Instead, either throw a more derived type that already exists in the Framework, or create your own type that derives from Exception.&lt;/P&gt;
&lt;P&gt;The following list examples of when you should throw specific exceptions:&lt;/P&gt;
&lt;P&gt;When validating a parameter (including the &lt;EM&gt;value&lt;/EM&gt; parameter in the set accessor of a property) that:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;is a null reference&amp;nbsp;(&lt;STRONG&gt;Nothing&lt;/STRONG&gt; in Visual Basic)&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.argumentnullexception(vs.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.argumentnullexception(vs.80).aspx"&gt;System.ArgumentNullException&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;is outside of the allowable range of values (such as&amp;nbsp;an index for a Collection/List)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.ArgumentOutOfRangeException(vs.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.ArgumentOutOfRangeException(vs.80).aspx"&gt;System.ArgumentOutOfRangeException&lt;/A&gt;&lt;STRONG&gt; &lt;/STRONG&gt;(&lt;STRONG&gt;DO NOT&lt;/STRONG&gt; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.IndexOutOfRangeException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.IndexOutOfRangeException(v.80).aspx"&gt;System.IndexOutOfRangeException&lt;/A&gt;) &lt;/P&gt;
&lt;P&gt;is outside the allowable values for a enum&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.invalidenumargumentexception(vs.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.componentmodel.invalidenumargumentexception(vs.80).aspx"&gt;System.ComponentModel.InvalidEnumArgumentException&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;contains a format that not meet the parameter specifications of a method (such as the format string for ToString(String))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.formatexception(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.formatexception(v.80).aspx"&gt;System.FormatException&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;is otherwise invalid (such as an empty string)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.ArgumentException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.ArgumentException(v.80).aspx"&gt;System.ArgumentException&lt;/A&gt;&lt;BR&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;When an operation is invalid for an object's current state:&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.invalidoperationexception(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.invalidoperationexception(v.80).aspx"&gt;System.InvalidOperationException&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;When an operation is performed on an object that has been disposed:&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.ObjectDisposedException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.ObjectDisposedException(v.80).aspx"&gt;System.ObjectDisposedException&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;When an operation is&amp;nbsp;not supported (such as&amp;nbsp;in an overridden&amp;nbsp;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.io.stream.write(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.io.stream.write(v.80).aspx"&gt;Stream.Write&lt;/A&gt;&amp;nbsp;in a &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.io.stream(VS.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.io.stream(VS.80).aspx"&gt;Stream&lt;/A&gt; opened for reading):&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.NotSupportedException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.NotSupportedException(v.80).aspx"&gt;System.NotSupportedException&lt;/A&gt;&amp;nbsp;(&lt;STRONG&gt;DO NOT&lt;/STRONG&gt; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.NotImplementedException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.NotImplementedException(v.80).aspx"&gt;System.NotImplementedException&lt;/A&gt;)&lt;/P&gt;
&lt;P&gt;When a conversion would result in an overflow (such as in a explicit cast operator overload):&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/System.OverflowException(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/System.OverflowException(v.80).aspx"&gt;System.OverflowException&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;For all other situations, consider creating your own type that derives from Exception and throwing that.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Note:&lt;/STRONG&gt;&amp;nbsp;Exceptions&amp;nbsp;that derive from &lt;STRONG&gt;ArgumentException&lt;/STRONG&gt; (including &lt;STRONG&gt;ArgumentNullException&lt;/STRONG&gt;, &lt;STRONG&gt;ArgumentException&lt;/STRONG&gt;,&lt;STRONG&gt; ArgumentOutOfRangeException &lt;/STRONG&gt;and &lt;STRONG&gt;InvalidEnumArgumentException&lt;/STRONG&gt;),&amp;nbsp;&lt;STRONG&gt;InvalidOperationException&lt;/STRONG&gt; (including &lt;STRONG&gt;ObjectDisposedException&lt;/STRONG&gt;) and &lt;STRONG&gt;NotSupportedException&lt;/STRONG&gt;&amp;nbsp;should only be thrown in situations that are avoidable (such as passing a null argument) and if thrown, would indicate a bug in the calling code. The &lt;A class="" href="http://msdn2.microsoft.com/en-us/library/system.io.path(v.80).aspx" mce_href="http://msdn2.microsoft.com/en-us/library/system.io.path(v.80).aspx"&gt;Path&lt;/A&gt; class &lt;EM&gt;is not&lt;/EM&gt; a good example of this, it incorrect throws&amp;nbsp;ArgumentException&amp;nbsp;to indicate that a&amp;nbsp;path is incorrectly formed, however, it does not expose any methods that can help prevent this from occurring.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1430357" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx">Exceptions</category></item><item><title>FAQ: Why does FxCop warn against catch(Exception)? - Part 3 [Nick Guerrera]</title><link>http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx</link><pubDate>Tue, 20 Jun 2006 07:16:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:638453</guid><dc:creator>nicholg</dc:creator><slash:comments>22</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/638453.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=638453</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=638453</wfw:comment><description>&lt;P&gt;&lt;EM&gt;This&amp;nbsp;is the third installment&amp;nbsp;in a&amp;nbsp;three-part series on why FxCop warns against catch(Exception):&lt;BR&gt;&lt;/EM&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)? - Part 1&lt;/A&gt;&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 2&lt;/A&gt;&lt;BR&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 3&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;I said from the &lt;A href="http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx"&gt;beginning&lt;/A&gt; that this issue is controversial, and some of your &lt;A href="http://blogs.msdn.com/fxcop/archive/2006/06/17/635835.aspx#637002" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/17/635835.aspx#637002"&gt;feedback&lt;/A&gt; certainly confirms that. I want to make it clear that I respect everyone’s right to disagree with me, to exclude or disable the FxCop warning, and to implement a less rigid exception policy in their &lt;I style="mso-bidi-font-style: normal"&gt;application code &lt;/I&gt;as they see fit. Furthermore, all of the opinions I’ve expressed are entirely my own and they represent a stricter interpretation of the Framework Design Guidelines&amp;nbsp;than absolutely required. I believe strongly that the approach that I’ve outlined helps to find and fix defects sooner during development and testing and therefore helps to deliver more robust production software. Nevertheless, the choice to adopt a different strategy for your application is entirely yours. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;On the other hand, as I mentioned in the previous post, this FxCop rule&amp;nbsp;is of paramount importance for &lt;I style="mso-bidi-font-style: normal"&gt;class library code&lt;/I&gt;. When class libraries call back to application code (via virtual methods, interface methods, events, or delegates), it is disastrous if they swallow or disguise the arbitrary exceptions that may be raised. (To see just how aggravating this can be for library consumers, witness the frustration with the WebBrowser control that readers expressed earlier.) Furthermore, class library code cannot make any assumptions about the implications of arbitrary exceptions for application code further up the call stack, so it must let them go so that the application can decide how to handle them. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;With that said, let me now address the most recent comments with more of my own personal opinions. :)&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: &lt;I style="mso-bidi-font-style: normal"&gt;“… I hope you do realize that a number of the recommendations you have given to people, myself included, basically amount to ‘let your app crash, your customers and business partners be damned.’ …" &lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;No, I most certainly do not realize that, because it’s simply not true. Applications which catch only the precise exceptions that they are prepared to handle gracefully are easier to develop and maintain over the long run. When this strategy is combined with strong testing, the application will ship with fewer defects and there will be very few unhandled exceptions in the wild. The truth is that once an unexpected exception has been raised in your application, customers and partners are likely to suffer one way or another. The impact of corrupt program state can range from security vulnerability to plain incorrect output, which can end up costing customers more time and money than dealing with a crash. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Please don’t misconstrue this to mean that it’s ok to write sloppy software which crashes regularly in common cases. Clearly that’s unacceptable and I never said otherwise. However, the right way to avoid this fate is to design your software carefully, implement it methodically, and test it thoroughly. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: &lt;I style="mso-bidi-font-style: normal"&gt;“I think all of your readers know full well that applications need to be designed to fail fast and do so clearly in order to ease the diagnosis and correction of software defects.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;I’m glad that we agree that applications should be designed to “fail fast.” From my point of view, once an exception is raised in your application and you haven’t decided exactly how to handle it on its own terms, then the application has already “failed” and process termination represents the “fast” part of the equation. :)&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: “&lt;I style="mso-bidi-font-style: normal"&gt;We do this, however, by modularizing software and testing it at an appropriate level of granularity, not by letting the whole app fall down in production.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;I do agree that it’s possible to sandbox components (for example, by using separate app-domains and ensuring that all shared data is immutable) such that the failure of a single component does not imply failure for the entire process. This then becomes, as you say, a question of granularity. You can think of each component as a mini-process, and then apply all of my arguments on a component-by-component basis. That is to say that when an isolated component throws an unexpected exception, its state must be discarded and it must be reloaded before it can reliably be consumed again. If your application is hardened in this way, then you would in fact&amp;nbsp;need to catch (Exception) in all of the locations that you mentioned in your first comment in order&amp;nbsp;to save the host process from termination. I missed that in my original reply and I apologize.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;However, I’d like to point out that it’s difficult and costly to implement this strategy correctly and I still&amp;nbsp;maintain that the approach I've been recommending is perfectly viable for most applications.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: &lt;I style="mso-bidi-font-style: normal"&gt;“Your recommendations will only work well for the people replying to your posts if you are interested in personally supporting their products when deployed in the field.”&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Given the choice between an application that abuses catch (Exception) and another which catches only the exceptions&amp;nbsp;that it can handle gracefully, I'd choose to support the second application in a heart-beat.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: &lt;I style="mso-bidi-font-style: normal"&gt;“Their customers and partners don't care about what FxCop might have recommended to the developer.”&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Not directly, but they do care about software quality, reliability, and overall correctness. Let’s take two familiar examples: Microsoft Excel and the C# compiler. I don’t want Excel to crash, but it would be much worse if it ever produced an incorrect value in a spreadsheet that I use to manage my personal finances. Similarly, I prefer to see the compiler ICE (Internal Compiler Error) in favor of emitting bad code. Of course, in an ideal world, Excel would never crash and the compiler would never ICE, but I’ve seen both happen and I’m sure it was in my best interest even if it upset me at the time. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Niall: &lt;I style="mso-bidi-font-style: normal"&gt;“I think you have to realise that exception handling is not just for the developer's benefit. The application is there to serve the user, not the developer, so simply bombing out because you can't display a new form is going to require the user to restart the application more than is really required.”&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Yes, the application is there to serve the user, not the developer and I couldn’t agree more. In that situation, you have to ask yourself why you couldn’t display the form.&amp;nbsp;If the only reason you can provide is that your catch (Exception) block was hit, then you really have no idea whether it’s in the best interest of the user to continue executing. For example, what if one of your assemblies just failed to load, or you’ve run out of memory, or the root cause is actually a corruption in shared data? What will go wrong next? On the other hand, if you catch (IOException) while trying to read the CSV file&amp;nbsp;that will ultimately be used to populate the new form, then you know beyond a shadow of a doubt that the correct application behavior is to inform the user that the file could not be opened and allow them to continue to use the application which remains in a consistent state.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Niall: &lt;I style="mso-bidi-font-style: normal"&gt;“In a previous project I worked on, we had a lot of places where exceptions were caught and error reports were sent back to the dev team. Therefore, the exceptions weren't just disappearing quietly, they were known to the developers. &lt;BR&gt;&lt;BR&gt;In many circumstances, the application would then continue. This is because a lot of our handling of exceptions would result in the form the exception came from closing or not appearing. Basically that element of the workflow did not occur. For almost all cases, this left the application in a state that allowed it to continue without problems. Of course, it wasn't 100% perfect. We did have a threshold of a certain number of exception reports in a certain amount of time would cause the handler to exit the app, though.”&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Both exception logging and using an exception count threshold are popular approaches for applications which catch all exceptions in places. In fact, this is actually exactly how the current version of FxCop works. Our experience with FxCop’s current exception handling policy has not been positive at all and so I would not suggest this approach universally. Nevertheless, as the application developer, it’s up to you to choose the appropriate policy.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Niall: &lt;I style="mso-bidi-font-style: normal"&gt;“Especially when the environment is such that you cannot deploy a fix at a day's notice, having the users restarting the application frequently when you could provide better is basically developing to suit the developers instead of the users.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;If it turns out that your application is crashing “frequently” enough that users have to restart it all the time, then it wasn’t ready to ship in the first place. By the time you’re ready to ship, you should have uncovered nearly all of the exceptions that you need to handle or prevent, and those that remain should be extremely uncommon. Besides careful testing on your end during the development cycle, it’s important to provide regular preview releases for your customers and partners to pound on. During that time, it will be invaluable that exception-related bugs are easy to diagnose and fix quickly. By the time you ship, you will have software which neither crashes nor swallows bugs!&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Niall: &lt;I style="mso-bidi-font-style: normal"&gt;“Of course, it can make diagnosing problems more difficult, but the role of the software is to make the user's job easier, not the developer's. Sometimes the developer just has to work a bit harder because otherwise the work goes on the user's plate.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;I never meant to imply that ease of diagnosis is an end unto itself. It is a means through which you can deliver better software with fewer defects, which most definitely serves to make the user’s life easier. Indeed, the developer often does have to work harder: uncovering the specific exceptions that need to be handled and the additional code that needs to be in place to prevent the others is often harder than slapping in an additional catch (Exception) block, but it’s the right thing to do…&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=638453" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx">Exceptions</category></item><item><title>FAQ: Why does FxCop warn against catch(Exception)? - Part 2 [Nick Guerrera]</title><link>http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx</link><pubDate>Sun, 18 Jun 2006 08:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:635835</guid><dc:creator>nicholg</dc:creator><slash:comments>28</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/635835.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=635835</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=635835</wfw:comment><description>&lt;P&gt;&lt;EM&gt;This&amp;nbsp;is the second installment&amp;nbsp;in a&amp;nbsp;three-part series on why FxCop warns against catch(Exception):&lt;BR&gt;&lt;/EM&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)? - Part 1&lt;/A&gt;&lt;BR&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 2&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 3&lt;/A&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;On Wednesday, I explained why catch (Exception) is a &lt;A href="http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/14/631923.aspx"&gt;bad idea&lt;/A&gt;, and many of you replied with interesting comments. Since it was too cumbersome to write everything I had to say in the comments section, I’ve replied to your comments below. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;K: &lt;I style="mso-bidi-font-style: normal"&gt;“Do you have a list of the 'unchecked' exceptions?”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;No. I don’t have a definitive list of the exceptions which should always be prevented and never caught. The best advice I can offer here is to follow the API documentation on a case-by-case basis. Don’t immediately assume that you need catch blocks for each of the exceptions that are listed in the documentation for a particular API. Instead, for each exception, ask you yourself if there’s a simple way to prevent it. If there is, then put in the preventative code and leave out the catch block. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;This can be tough to discern in some circumstances. For example, it may seem that a call to File.Exists can prevent FileNotFoundException, but there’s a race condition where the operation will still fail if the file is deleted in between the time File.Exists returns true and the call is initiated. Also, there are some methods in the .NET Framework which misuse exception types. For example, in an ideal world, you should never catch ArgumentException, but Enum.Parse throws it to indicate that the input does not represent one of the enum values. Since there’s no way to prevent that short of rewriting Enum.Parse, you have to consider ArgumentException ‘checked’ in that specific case. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;If you’re unsure about a specific API, try asking for help on &lt;A href="http://forums.microsoft.com/" mce_href="http://forums.microsoft.com"&gt;http://forums.microsoft.com&lt;/A&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Gill: &lt;I style="mso-bidi-font-style: normal"&gt;“… there is one issue that troubles me: what if my program handles 10 specific exceptions I defined in the same way (when all these exceptions hint at different problems that are related), in this case I would want some catch blocks to perform the same code. If these exceptions don't share a common base, there is no way to make all of them perform the same code short of using the same lines (or method call) in each catch block.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;I don’t think there are that many methods out there which throw 10 unrelated and unpreventable exceptions, but I can relate to your frustration with API which throw unrelated exceptions to indicate related failures. Refactoring the shared handler code in to a helper method is the simplest way to deal with the issue. If you find yourself catching the same set of exceptions from the same method in more than one place, consider refactoring out the entire try/catch/catch/… series in to a helper method of its own.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Gill: “&lt;I style="mso-bidi-font-style: normal"&gt;Furthermore, if all developers worked correctly and used this guideline my code might be facing unexpected exceptions from 3rd party code bugs. If this 3rd party isn't available to fix the bug, my program can fail at unexpected times.”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;I don’t follow the reasoning here. If you hit a bug in 3&lt;SUP&gt;rd&lt;/SUP&gt; party code, then catch (Exception) is just going to make things worse. You should treat this unhandled exception no different from any other. It may turn out that you can work around the problem by using the API differently or adding a catch handler for the specific exception type that was raised if it turns out to be recoverable. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Richard: &lt;I style="mso-bidi-font-style: normal"&gt;“Of course, it would help if the Exception class was abstract, and the Framework never threw a general Exception! For example, try calling System.Drawing.ColorTranslator.FromHtml("#aaax")”&lt;/I&gt;&lt;BR style="mso-special-character: line-break"&gt;&lt;BR style="mso-special-character: line-break"&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Youch! You’re right; it’s absolutely terrible to throw an instance of System.Exception and FxCop warns against that as well. I will do my best to argue for reopening and fixing this bug!&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Anonymous: &lt;I style="mso-bidi-font-style: normal"&gt;“If my program has a feature to auto-save unsaved work every 10 minutes, and the auto-save procedure (which may use a third-party library) throws an unchecked exception, are you suggesting that the program should simply crash out, because this exception must not be handled?”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Yes, that is in fact exactly what I’m suggesting. A bug is a bug is a bug. It doesn’t matter if it’s in your code or 3&lt;SUP&gt;rd&lt;/SUP&gt; party code, catch (Exception) will just make it harder to find and fix. Having an auto-save feature is a great way to mitigate the risk of data loss. However, if the auto-save code itself is buggy, then all bets are off.&amp;nbsp;It’s sort of like discovering that your parachute won’t open…&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeff: &lt;I style="mso-bidi-font-style: normal"&gt;“Well here is my slight problem with this. The best example I can give you as to why you absolutely have [to] Catch(Exception ex) to do this is a bug I reported back in Beta 2 … The Browser Control …”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;I style="mso-bidi-font-style: normal"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Chris: &lt;I style="mso-bidi-font-style: normal"&gt;“… the WebBrowser control will silently eat the NullReferenceException. I want to know when if my code has a bug, but the WebBrowser hides my bugs&lt;/I&gt;.”&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;That’s awful! I will talk to the owners of the Web browser control about getting this fixed. One of the things that I failed to mention in my original post is that it’s much worse to swallow exceptions in library code than in application code. If you’re developing a reusable library, this rule (like many others in FxCop) is doubly important!&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Pop Catalin Sever:&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;I style="mso-bidi-font-style: normal"&gt;“Let the entire application crash? Are you kidding? That's really unacceptable. And btw if you notify the user that something bad has happened in most cases he will help out recover the application or some data. If you just let the application close I think that very likely you're going to be in big trouble.”&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Let the application continue to run in&amp;nbsp;an indeterminate state and carelessly corrupt data? Are you kidding? ;) &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;While I agree that the user should be notified that something has gone wrong, I don’t agree that the application should continue running afterwards. Instead, a friendly dialog should allow the user to send an error report before the application closes. If data loss is a potential issue in your application, then implement an auto-save feature. It’s worth noting that this is exactly the approach that’s used&amp;nbsp;in Microsoft Office.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Beginning in .NET 2.0, you don’t need to implement the crash dialog box yourself. If you let the exception go unhandled, a default dialog box will be displayed that allows the user to send an error report to Microsoft. &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;Note that for Windows Forms applications, you need to call Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException) to make this work.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;If you prefer, you can hook the AppDomain.UnhandledException event and display your own dialog that allows users to send a bug report directly to you.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Jeremy: &lt;EM&gt;“Here are another three cases where you absolutely must catch Exception (and at the very least provide for adequate logging, even if you then rethrow): &lt;BR style="mso-special-character: line-break"&gt;&lt;BR style="mso-special-character: line-break"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in"&gt;&lt;I style="mso-bidi-font-style: normal"&gt;1. In every method you spin up in a new thread. &lt;BR&gt;2. In every method called arbitrarily on threads you don't control (ie. WebMethods, methods exposed via remoting, delegates passed across remoting, windows service control overrides, etc.) &lt;BR&gt;3. &lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;st1:place w:st="on"&gt;Main&lt;/st1:place&gt; &lt;BR&gt;&lt;BR&gt;Seeing a pattern here? :)”&lt;o:p&gt;&lt;/o:p&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;It sounds like you got burned by the default exception handling policy for secondary threads in .NET 1.0 and 1.1. Thankfully, the situation is vastly improved in .NET 2.0. See&amp;nbsp;&lt;A href="http://msdn2.microsoft.com/en-us/library/ms228965.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms228965.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms228965.aspx&lt;/A&gt;. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;If you can’t move to 2.0 just yet, then a better approach than sprinkling catch (Exception) in all of these places is to hook AppDomain.UnhandledException (and Application.ThreadException for Windows Forms applications). In your handler, you can throw up a dialog like the default dialog in .NET 2.0 and call Environment.Exit afterwards.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=635835" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx">Exceptions</category></item><item><title>FAQ: Why does FxCop warn against catch(Exception)? - Part 1 [Nick Guerrera]</title><link>http://blogs.msdn.com/fxcop/archive/2006/06/14/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-1-_5B00_Nick-Guerrera_5D00_.aspx</link><pubDate>Thu, 15 Jun 2006 09:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:631923</guid><dc:creator>nicholg</dc:creator><slash:comments>26</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/631923.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=631923</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=631923</wfw:comment><description>&lt;P&gt;&lt;EM&gt;This&amp;nbsp;is the first&amp;nbsp;installment&amp;nbsp;in a&amp;nbsp;three-part series on why FxCop warns against catch(Exception):&lt;/EM&gt;&lt;BR&gt;FAQ: Why does FxCop warn against catch(Exception)? - Part 1&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/17/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-2-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 2&lt;/A&gt;&lt;BR&gt;&lt;A class="" href="http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx" mce_href="http://blogs.msdn.com/fxcop/archive/2006/06/19/FAQ_3A00_-Why-does-FxCop-warn-against-catch_2800_Exception_29003F00_-_2D00_-Part-3-_5B00_Nick-Guerrera_5D00_.aspx"&gt;FAQ: Why does FxCop warn against catch(Exception)?&amp;nbsp;- Part 3&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;This question comes up a lot, and I think there’s a lot of confusion and controversy about the rule. Many people seem to think that it’s noisy, annoying, and not worth their time. I find that very unfortunate because following its guidance can really help you build better software. Here’s my attempt to convince the Nay-Sayers that catch(Exception) is almost always a terrible idea.&lt;/P&gt;
&lt;P&gt;I’ve heard a lot of arguments against DoNotCatchGeneralExceptionTypes, but they generally boil down to one or both of the following:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;“If I call an API which doesn’t document which exceptions it can throw, then I have no choice but to catch all possible exceptions!” 
&lt;LI&gt;“My application needs to be robust; it can’t crash just because of an exception that I forgot to handle!”&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Both arguments are ill-conceived because they presume that you can handle an exception even if you don’t know what failure it represents or how it impacts the code that will execute next. There’s an implicit assumption in both arguments that the mere act of catching an exception is equivalent to handling it appropriately, which simply isn’t true. &lt;/P&gt;
&lt;P&gt;For the sake of the discussion to follow, exceptions can be divided in to two general categories: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Those that signal bugs (i.e. API misuse), or catastrophic system failures&lt;BR&gt;(e.g. ArgumentException, NullReferenceException, ExecutionEngineException.) 
&lt;LI&gt;Those that signal unpreventable yet recoverable error conditions.&lt;BR&gt;(e.g. FileNotFoundException, SocketException.)&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;In languages like Java, which have a notion of checked exceptions, only exceptions of the second kind are eligible to be checked while exceptions of the first kind must be left unchecked. On that basis and for lack of better terminology, I will hereafter refer to exceptions in the first bucket as ‘unchecked’ and exceptions in the second bucket as ‘checked’. (Note the quotes.) I am using the terms loosely to mean that if exceptions were checked in .NET, then those in the first bucket would remain unchecked while those in the second bucket could become checked.&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;Independent of where you stand on the checked exception debate, this distinction is very important to hold in your mind when writing exception handling code. In particular, do you really ever want to catch the ‘unchecked’ exceptions at all? Probably not! Instead, you should prevent them from occurring in the first place. For example, check if a variable is null before dereferencing it, don’t catch (NullReferenceException).&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;If a bug in your code manifests itself as an exception (and most of the ‘unchecked’ exceptions are just that), then it’s best to let the application terminate right at the point of failure. When the bug is discovered, you’ll get exactly the diagnostic information you need to fix it quickly, add the appropriate regression tests, and move on. On the other hand, debugging a system which aggressively swallows general exceptions feels like you’re one of the police investigators in “CSI: Miami”. The culprit has left the crime scene and you have to work backwards from the subtle clues in your program’s incorrect behavior.&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;And that’s the principal reason to avoid catch (Exception): it hides bugs in your code. These bugs are far more costly to resolve and since they can go undetected, the quality of your software suffers. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;Some readers might be thinking that it would be helpful if the .NET Framework exception class hierarchy reflected the ‘checked’ vs. ‘unchecked’ nature of exceptions. In fact, it’s rather seductive to imagine a base class, say System.CheckedException, for all of the ‘checked’ exceptions. The argument follows that you could then replace catch (Exception) with catch (CheckedException)… and the problem would be solved since you no longer catch those insidious ‘unchecked’ exceptions. Unfortunately, this approach would be just as flawed. FxCop would still warn against catch (CheckedException) because it’s still far too general. By definition, ‘checked’ exceptions are unpreventable and must therefore be caught. Nevertheless, catching them is the easy part; the hard part is deciding what to do next, and that decision is purely a function of the specific exception type and it’s meaning in the given context. For example, to recover from FileNotFoundException for your configuration file, you might use a default configuration, and to recover from a SocketException, you might fall back to a local store until the network becomes available again. However, if you don’t know what to do about it, then let it go.&lt;BR&gt;&lt;BR&gt;Returning to the first argument against this rule, please don’t get me wrong, I definitely empathize with the need for accurate exception information in API documentation. However, in the absence of such documentation, the safest approach is to treat all API as &lt;BR&gt;“innocent until proven guilty.” Wait until you have hard evidence that a particular exception can be raised before you attempt to handle it. &lt;BR&gt;&lt;/P&gt;
&lt;P&gt;Also, be sure to test your exception handling code. For example, delete a file out from underneath your application while it’s running and see how it deals with FileNotFoundException. It’s simply not sufficient to write optimistic and untested code, just because the exception is mentioned in the API documentation. I can’t tell you how many times I’ve seen untested catch handlers fail to work as expected. The classic example is a formatting exception raised in the error message that’s never provoked from a test. Spurious and unnecessary catch (Exception) blocks can completely torpedo your code coverage! (Remember: if it’s not tested, it’s broken.)&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;One important thing to note is that if you follow our advice and catch fewer exceptions, then exceptions will naturally travel further up the call stack. Sometimes they’ll terminate the application, but other times someone further up the call stack will know what to do and your only job is to clean up after yourself in finally blocks as the stack unwinds.&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;To summarize, here are some important things to consider when dealing with exceptions in managed code: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Do not catch preventable (‘unchecked’) exceptions. &lt;BR&gt;Only catch the specific unpreventable (‘checked’) exceptions that you are certain can be raised and that you know how to handle. 
&lt;LI&gt;Test your exception handling code. 
&lt;LI&gt;Use finally blocks liberally for clean-up code to keep your state as consistent as possible in case an exception that you cannot handle needs to be handled further up the stack.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;And finally, I’d like to point out that our team has learned this lesson the hard way. There are actually far too many catch (Exception) blocks in FxCop itself and it has caused us a great deal of grief. We’re working on removing them and I’m confident it will help us to deliver better software!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=631923" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx">Exceptions</category></item><item><title>FAQ: Why do some sources recommend extending ApplicationException while FxCop does not? [Michael Fanning, David Kean]</title><link>http://blogs.msdn.com/fxcop/archive/2006/04/05/faq-why-do-some-sources-recommend-extending-applicationexception-while-fxcop-does-not-michael-fanning-david-kean.aspx</link><pubDate>Thu, 06 Apr 2006 06:21:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:569569</guid><dc:creator>David M. Kean</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/fxcop/comments/569569.aspx</comments><wfw:commentRss>http://blogs.msdn.com/fxcop/commentrss.aspx?PostID=569569</wfw:commentRss><wfw:comment>http://blogs.msdn.com/fxcop/rsscomments.aspx?PostID=569569</wfw:comment><description>&lt;P&gt;&lt;EM&gt;&lt;A class="" href="http://msdn2.microsoft.com/en-us/library/ms182171.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/ms182171.aspx"&gt;TypesShouldNotExtendCertainBaseTypes&lt;/A&gt;&lt;/EM&gt;&lt;EM&gt; fires on types that derive from ApplicationException and &lt;/EM&gt;&lt;A class="" href="http://msdn2.microsoft.com/en-gb/library/ms182338.aspx" mce_href="http://msdn2.microsoft.com/en-gb/library/ms182338.aspx"&gt;&lt;EM&gt;DoNotRaiseReservedExceptionTypes&lt;/EM&gt;&lt;/A&gt;&lt;EM&gt;&amp;nbsp;on members that throw ApplicationException. Why?&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;There are several outdated documents floating around on the web (some orginally published by Microsoft) recommending that application developers extend ApplicationException. However, this guidance was revised several years ago for a couple of reasons:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;It deepens the class hierarchy and presents additional complications for consumers writing exception handlers without providing additional value. 
&lt;LI&gt;It prevents the reuse of existing exception classes defined in the Base Class Library (BCL).&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The official &lt;A href="http://msdn2.microsoft.com/en-US/library/ms229042(VS.80).aspx" mce_href="http://msdn2.microsoft.com/en-US/library/ms229042(VS.80).aspx"&gt;Microsoft Design Guidelines&lt;/A&gt;,&amp;nbsp;particularly the &lt;A href="http://msdn2.microsoft.com/en-US/library/ms229007(VS.80).aspx" mce_href="http://msdn2.microsoft.com/en-US/library/ms229007(VS.80).aspx"&gt;Catching and&amp;nbsp;Throwing&amp;nbsp;Standard Exception Types&lt;/A&gt; section,&amp;nbsp;have only recently been revamped to include this new information. Both Krzysztof Cwalina and Brad Abrams, who own the &lt;A class="" href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756" mce_href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756"&gt;Framework Design Guidelines&lt;/A&gt; at Microsoft, have both blogged on this issue (&lt;A class="" href="http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx" mce_href="http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx"&gt;ApplicationException considered useless&lt;/A&gt; and &lt;A class="" href="http://blogs.msdn.com/brada/archive/2004/03/25/96251.aspx" mce_href="http://blogs.msdn.com/brada/archive/2004/03/25/96251.aspx"&gt;Introducing the .NET Framework Standard Library Annotated Reference&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;If you see any MSDN samples or articles that&amp;nbsp;use ApplicationException (or&amp;nbsp;violate any FxCop rule, for that matter), please file a bug on the &lt;A class="" href="http://connect.microsoft.com/site/sitehome.aspx?SiteID=210" mce_href="http://connect.microsoft.com/site/sitehome.aspx?SiteID=210"&gt;Microsoft Connect&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=569569" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/fxcop/archive/tags/FAQ/default.aspx">FAQ</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Framework+Design+Guidelines/default.aspx">Framework Design Guidelines</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Rules/default.aspx">Rules</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Team+System/default.aspx">Team System</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Code+Analysis/default.aspx">Code Analysis</category><category domain="http://blogs.msdn.com/fxcop/archive/tags/Exceptions/default.aspx">Exceptions</category></item></channel></rss>