#897 – Rules for Creating a Custom Exception Type

The basic guidelines for defining your own custom exception types are as follows:

  • Derive from one of the following:
  • 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.

 

 

Advertisement