ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

GetMostInner

Gets the most inner (deepest) exception of a given Exception object

Source

/// <summary>
/// Gets the most inner (deepest) exception of a given Exception object
/// </summary>
/// <param name="ex">Source Exception</param>
/// <returns></returns>
public static Exception GetMostInner(this Exception ex)
{
    Exception ActualInnerEx = ex;

    while (ActualInnerEx != null)
    {
        ActualInnerEx = ActualInnerEx.InnerException;
        if (ActualInnerEx != null)
            ex = ActualInnerEx;
    }
    return ex;
}

Example

Exception ex = new Exception("Text1", new Exception("Text2", new Exception("Text3", new Exception("Text4"))));
Exception ex2 = ex.GetMostInner();

Author: Jonnidip

Submitted on: 28 mei 2009

Language: C#

Type: System.Exception

Views: 5362