#897 – Rules for Creating a Custom Exception Type
July 29, 2013 2 Comments
The basic guidelines for defining your own custom exception types are as follows:
- Derive from one of the following:
- System.Exception
- One of the common predefined system exception types in .NET (e.g. ArgumentException, FormatException)
- End the name of your type with “Exception” (e.g. InvalidDogBarkException)
- Mark your exception type as Serializable
- Define the following constructors:
- public MyException()
- public MyException(string message) : base(message)
- public MyException(string message, Exception inner) : base(message, inner)
- public MyException(SerializationInfo info, StreamingContext context) : base(info, context)
- Consider adding custom data:
- Add one or more custom properties
- Add one or more constructor that accept values for the custom properties
Click here for a full example.