#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

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

2 Responses to #897 – Rules for Creating a Custom Exception Type

  1. Pingback: Dew Drop – July 29, 2013 (#1,594) | Alvin Ashcraft's Morning Dew

  2. Szél Péter says:

    Hi Sean, thanks for the great content.
    Note that the constructor with SerializationInfo doesn’t have to be public.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: