{"id":114,"date":"2014-06-06T12:19:14","date_gmt":"2014-06-06T19:19:14","guid":{"rendered":"http:\/\/www.kolls.net\/blog\/?p=114"},"modified":"2014-06-06T12:19:14","modified_gmt":"2014-06-06T19:19:14","slug":"generating-compiler-warnings-on-notimplementedexception","status":"publish","type":"post","link":"https:\/\/www.kolls.net\/blog\/?p=114","title":{"rendered":"Generating compiler warnings on NotImplementedException"},"content":{"rendered":"<p>In .NET, whenever an interface is generated, the method stubs are filled in with &#8220;NotImplementedException&#8221;s.  We might also use these exceptions elsewhere, for example, constructing pre-test class methods, or deferring implementation of some functionality.  For actual circumstances where some behavior cannot be performed, <a href=\"http:\/\/blogs.msdn.com\/b\/brada\/archive\/2004\/07\/29\/201354.aspx\">use NotSupportedException instead<\/a>.<\/p>\n<p>It&#8217;s easy to do a search for NotImplementedException, or use the VS Code Analysis tool to detect uses, but these both require a special effort, and NotImplementedExceptions may be lost or accidentally slip into production builds.<\/p>\n<p>To avert this, here is an easy way to cause the compiler to generate a warning everytime it encounters a NotImplementedException.  Add this code to a file anywhere in your project.<\/p>\n<pre class=\"prettyprint\">\r\nnamespace System\r\n{\r\n#if DEBUG\r\n    [Obsolete(\"Replace NotImplementedException with NotSupportedException, or implement\")]\r\n    public class NotImplementedException : Exception\r\n    {\r\n        public NotImplementedException() : base() { }\r\n        public NotImplementedException(string message) : base(message) { }\r\n    }\r\n#endif\r\n}\r\n<\/pre>\n<p>We use the System namespace to &#8220;override&#8221; the default implementation of NotImplementedException.  We use the Obsolete attribute because it generates a compiler warning automatically.  In other words, these are bad things to do in general, but serve a pragmatic purpose here.<\/p>\n<p>Unfortunately, we cannot make partial classes across assemblies, so there will be an additional warning that the NotImplementedException defined here conflicts with the mscorlib.dll&#8217;s implementation.  However, since the overall goal is to generate warnings, this is acceptable.  These warnings will disappear once all NotImplementedException references are removed.<\/p>\n<p>Every case where a NotImplementedException is used, whether by an auto-generated stub or some other reason, will produce this compiler warning:<\/p>\n<pre class=\"prettyprint\">\r\nWarning\t27\t'System.NotImplementedException' is obsolete: 'Replace NotImplementedException with NotSupportedException, or implement'\r\n<\/pre>\n<p>In addition, when in a code file using NotImplementedException, a warning marker will be shown.  Mousing over suggests to change the exception.<\/p>\n<p><a href=\"http:\/\/www.kolls.net\/blog\/wp-content\/uploads\/2014\/06\/notimplementedexception.png\"><img loading=\"lazy\" src=\"https:\/\/www.kolls.net\/blog\/wp-content\/uploads\/2014\/06\/notimplementedexception.png\" alt=\"notimplementedexception\" width=\"860\" height=\"89\" class=\"alignnone size-full wp-image-116\" srcset=\"https:\/\/www.kolls.net\/blog\/wp-content\/uploads\/2014\/06\/notimplementedexception.png 860w, https:\/\/www.kolls.net\/blog\/wp-content\/uploads\/2014\/06\/notimplementedexception-300x31.png 300w, https:\/\/www.kolls.net\/blog\/wp-content\/uploads\/2014\/06\/notimplementedexception-500x51.png 500w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><\/a><\/p>\n<p>Implementing the feature, or changing the exception to NotSupportedException, removes the warning.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In .NET, whenever an interface is generated, the method stubs are filled in with &#8220;NotImplementedException&#8221;s. We might also use these exceptions elsewhere, for example, constructing pre-test class methods, or deferring implementation of some functionality. For actual circumstances where some behavior &hellip; <a href=\"https:\/\/www.kolls.net\/blog\/?p=114\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/114"}],"collection":[{"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=114"}],"version-history":[{"count":3,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/114\/revisions"}],"predecessor-version":[{"id":118,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/114\/revisions\/118"}],"wp:attachment":[{"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}