#882 – What Types of Exceptions to Throw

When you throw an exception from your code, you should throw one of the following exception types:

  • One of the preexisting exception types, if the error matches the exception type (e.g. System.InvalidOperationException or System.ArgumentException)
  • System.ApplicationException
  • A custom exception type derived from System.Exception

You should not throw exceptions whose type is System.ExceptionSystem.SystemExceptionSystem.NullReferenceException or System.IndexOutOfRangeException.  These exceptions are thrown by code in the .NET Framework, but you should never throw these exceptions from your own code.

Advertisement

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

5 Responses to #882 – What Types of Exceptions to Throw

  1. Hi Sean

    That’s the reason not to throw these exceptions yourself?

    Cheers
    Michel

  2. Pingback: Dew Drop – July 8, 2013 (#1,580) | Alvin Ashcraft's Morning Dew

  3. Pingback: #890 – Common System Exception Types that You Might Throw | 2,000 Things You Should Know About C#

  4. Pingback: #891 – Example of Throwing a System Exception Type | 2,000 Things You Should Know About C#

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: