#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.

Advertisements