#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.
Pingback: Dew Drop – July 29, 2013 (#1,594) | Alvin Ashcraft's Morning Dew
Hi Sean, thanks for the great content.
Note that the constructor with SerializationInfo doesn’t have to be public.