System.Exception.GetBaseException Method

When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

Syntax

public virtual Exception GetBaseException ()

Returns

The first exception thrown in a chain of exceptions. If the Exception.InnerException property of the current exception is a null reference (Nothing in Visual Basic), this property returns the current exception.

Remarks

A chain of exceptions consists of a set of exceptions such that each exception in the chain was thrown as a direct result of the exception referenced in its InnerException property. For a given chain, there can be exactly one exception that is the root cause of all other exceptions in the chain. This exception is called the base exception and its InnerException property always contains a null reference.

For all exceptions in a chain of exceptions, the GetBaseException method must return the same object (the base exception).

Use the GetBaseException method when you want to find the root cause of an exception but do not need information about exceptions that may have occurred between the current exception and the first exception.

Example

The following example shows an implementation of the Exception.GetBaseException method.

C# Example

public virtual Exception GetBaseException() {
 Exception inner = InnerException;
 Exception back = this;
 while (inner != null) {
 back = inner;
 inner = inner.InnerException;
 }
 return back;
}
      

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0