skynyrd
10/27/2016 - 7:59 AM

ExceptionExtensions

ExceptionExtensions

#Exception #Extension

public static class Extensions
{
  //Usage: exception.GetInnerExceptions().Last().Message
    public static IEnumerable<Exception> GetInnerExceptions(this Exception ex)
    {
        if (ex == null)
        {
            throw new ArgumentNullException();
        }

        var innerException = ex;
        do
        {
            yield return innerException;
            innerException = innerException.InnerException;
        }
        while (innerException != null);
    }
}