using System.Collections.Generic;
public static void Main()
var stackedEx = new Queue<string>();
var testEx = new Exception("1st Exc.",
new AccessViolationException("2.nd Exc.",
new AggregateException("3rd Exc.",
new ApplicationException("4th Exc."))));
var innerEx = testEx.FlattenInnerException();
foreach(var e in innerEx)
Console.WriteLine(e.Message);
public static class ExceptionHelper
public static IEnumerable<Exception> FlattenInnerException(this Exception ex)
throw new ArgumentNullException("ex");
yield return innerException;
innerException = innerException.InnerException;
while (innerException != null);