GetMostInner

/// <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();

Description

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

Details

Double click on the code to select all.

 

;