<p>In a follow-up to a <a href="https://stackoverflow.com/questions/54789/what-is-the-correct-net-exception-to-throw-when-try-to-insert-a-duplicate-objec">previous question</a> regarding exceptions, what are best practices for creating a custom exception in .NET?  </p>

<p>More specifically should you inherit from System.Exception, System.ApplicationException or some other base exception?</p>

[{'answer_id': 54854, 'author': 'Thomas Owens', 'author_id': 572, 'author_profile': 'https://Stackoverflow.com/users/572', 'pm_score': 1, 'selected': False, 'text': '<p>I think the single most important thing to remember when dealing with exceptions at any level (making custom, throwing, catching) is that exceptions are only for exceptional conditions.</p>\n'}, {'answer_id': 54856, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 5, 'selected': True, 'text': '<p>Inherit from <code>System.Exception</code>. <code>System.ApplicationException</code> is useless and the design guidelines say "<strong>Do not</strong> throw or derive from <code>System.ApplicationException</code>." </p>\n\n<p>See <a href="http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/kcwalina/archive/2006/06/23/644822.aspx</a></p>\n'}, {'answer_id': 54859, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 1, 'selected': False, 'text': '<p>The base exception from where all other exceptions inherit from is System.Exception, and that is what you should inherit, unless of course you have a use for things like, say, default messages of a more specific exception.</p>\n'}, {'answer_id': 54863, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>There is a code snippet for it.  Use that.  Plus, check your code analysis afterwards; the snippet leaves out one of the constructors you should implement.  </p>\n'}, {'answer_id': 54942, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 5, 'selected': False, 'text': '<p>In the C# IDE, type \'exception\' and hit TAB.  This will expand to get you started in writing a new exception type.  There are comments withs links to some discussion of exception practices.</p>\n\n<p>Personally, I\'m a big fan of creating lots of small classes, at that extends to exception types.  For example, in writing the Foo class, I can choose between:</p>\n\n<ol>\n<li><code>throw new Exception("Bar happened in Foo");</code></li>\n<li><code>throw new FooException("Bar happened");</code></li>\n<li><code>throw new FooBarException();</code></li>\n</ol>\n\n<p>where</p>\n\n<pre><code>class FooException : Exception \n{\n    public FooException(string message) ... \n}\n</code></pre>\n\n<p>and</p>\n\n<pre><code>class FooBarException : FooException \n{\n    public FooBarException() \n        : base ("Bar happened") \n    {\n    }\n}\n</code></pre>\n\n<p>I prefer the 3rd option, because I see it as being an OO solution.</p>\n'}]
['https://Stackoverflow.com/questions/54851', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1768/']