Exception Hierarchy

 

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.

The System.Exception class 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 and no new members to it. For example, the InvalidCastException class hierarchy is as follows:

Object Exception SystemException InvalidCastException

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. If you are designing an application that creates new exceptions, you should derive those exceptions from the Exception class. It is not recommended that you catch a SystemException, nor is it good programming practice to throw a SystemException in your application.

The most severe exceptions — those thrown by the runtime or in nonrecoverable conditions — include ExecutionEngineException, StackOverflowException, and OutOfMemoryException.

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.

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 typeBase typeDescriptionExample
ExceptionObjectBase class for all exceptions.None (use a derived class of this exception).
SystemExceptionExceptionBase class for all runtime-generated errors.None (use a derived class of this exception).
IndexOutOfRangeExceptionSystemExceptionThrown by the runtime only when an array is indexed improperly.Indexing an array outside its valid range:

 var i = arr[arr.Length + 1];

 Dim i = arr(arr.Length + 1)
NullReferenceExceptionSystemExceptionThrown by the runtime only when a null object is referenced.object o = null; string s = o.ToString();

 Dim o As Object = Nothing Dim s As String = o.ToString()
AccessViolationExceptionSystemExceptionThrown by the runtime only when invalid memory is accessed.Occurs when interoperating with unmanaged code or unsafe managed code, and an invalid pointer is used.
InvalidOperationExceptionSystemExceptionThrown by methods when in an invalid state.Calling the enumerator's GetNext method after removing an item from the underlying collection.
ArgumentExceptionSystemExceptionBase class for all argument exceptions.None (use a derived class of this exception).
ArgumentNullExceptionArgumentExceptionThrown by methods that do not allow an argument to be null.String s = null; int i = "Calculate".IndexOf(s);

 Dim s As String = Nothing Dim i As Integer = "Calculate".IndexOf(s)
ArgumentOutOfRangeExceptionArgumentExceptionThrown by methods that verify that arguments are in a given range.String s = "string"; s = s.Substring(s.Length + 1);

 Dim s As String = "string" s = s.Substring(s.Length + 1)
ExternalExceptionSystemExceptionBase class for exceptions that occur or are targeted at environments outside the runtime.None (use a derived class of this exception).
COMExceptionExternalExceptionException encapsulating COM HRESULT information.Used in COM interop.
SEHExceptionExternalExceptionException encapsulating Win32 structured exception handling information.Used in unmanaged code interop.

Exception Class and Properties
Exception Handling Fundamentals
Best Practices for Exceptions
Exceptions

Show: