It seems that I started a little cue ball with the exception. Depending on the Microsoft Best Practices guide you follow ... you can either inherit from System.Exception or System.ApplicationException. There's a good (but old) blog post that tries to fix the confusion. I will save my example with Exception for now, but you can read the message and choose based on what you need:
http://weblogs.asp.net/erobillard/archive/2004/05/10/129134.aspx
No battle anymore! Thanks to Frederick for pointing out the FxCop CA1058 rule, which states that your Exceptions should inherit from System.Exception, not System.ApplicationException:
CA1058: Types Should Not Extend Some Base Types
Define a new class that inherits from Exception (I have included some constructors ... but you don't need them):
using System; using System.Runtime.Serialization; [Serializable] public class MyException : Exception {
And elsewhere in your code to throw:
throw new MyException("My message here!");
EDIT
Updated with changes to provide Serializable exceptions. Details can be found here:
Winterdom Blog Archive - Make Serializable Exception Classes
Check out the section on the steps to take if you add custom properties to the Exception class.
Thanks to Igor for calling me!
Justin Niessner Feb 04 '10 at 14:17 2010-02-04 14:17
source share