There are two types of exceptions: exceptions generated by an executing program, and exceptions generated by the common language runtime. In addition, there is a hierarchy of exceptions that can be thrown by either an application or the runtime.
Exception is the base class for exceptions. Several exception classes inherit directly from Exception, including ApplicationException and SystemException. These two classes form the basis for almost all runtime exceptions.
Most exceptions that derive directly from Exception add no functionality to the Exception class. For example, the InvalidCastException Class hierarchy is as follows:
The runtime throws the appropriate derived class of SystemException when errors occur. These errors result from failed runtime checks (such as array out-of-bound errors), and can occur during the execution of any method. An ApplicationException is thrown by a user program rather than by the runtime. If you are designing an application that creates new exceptions, you should derive those exceptions from the ApplicationException class. It is not recommended that you catch a SystemException, nor is it good programming practice to throw a SystemException in your application.
Interoperation exceptions derive from SystemException and are further extended by ExternalException. For example, COMException is the exception thrown during COM interop operations and derives from ExternalException. Win32Exception and SEHException also derive from ExternalException.
Hierarchy of Runtime Exceptions
The runtime has a base set of exceptions deriving from SystemException that it throws when executing individual instructions. The following table hierarchically lists the standard exceptions provided by the runtime and the conditions under which you should create a derived class.
|Exception type||Base type||Description||Example|
|Exception||Object||Base class for all exceptions.||None (use a derived class of this exception).|
|SystemException||Exception||Base class for all runtime-generated errors.||None (use a derived class of this exception).|
|IndexOutOfRangeException||SystemException||Thrown by the runtime only when an array is indexed improperly.||Indexing an array outside its valid range:
|NullReferenceException||SystemException||Thrown by the runtime only when a null object is referenced.|
|InvalidOperationException||SystemException||Thrown by methods when in an invalid state.||Calling |
|ArgumentException||SystemException||Base class for all argument exceptions.||None (use a derived class of this exception).|
|ArgumentNullException||ArgumentException||Thrown by methods that do not allow an argument to be null.|
|ArgumentOutOfRangeException||ArgumentException||Thrown by methods that verify that arguments are in a given range.|
|ExternalException||SystemException||Base class for exceptions that occur or are targeted at environments outside the runtime.||None (use a derived class of this exception).|
|ComException||ExternalException||Exception encapsulating COM HRESULT information.||Used in COM interop.|
|SEHException||ExternalException||Exception encapsulating Win32 structured exception handling information.||Used in unmanaged code interop.|