Table of Contents

ExceptionExtensions Class

Definition

Namespace
Cuemon.Extensions
Assemblies
Cuemon.Extensions.Core.dll
Source
src/Cuemon.Extensions.Core/ExceptionExtensions.cs

Extension methods for the Exception class.

public static class ExceptionExtensions
Inheritance
object
ExceptionExtensions

Examples

ExceptionExtensions.Flatten unwinds deeply nested exception hierarchies into a flat IEnumerable<Exception> while preserving insertion order. This example constructs a four-level exception chain starting with an InvalidOperationException("First") containing AmbiguousMatchException, OutOfMemoryException, and an inner AggregateException with an AccessViolationException. Key setup includes building the nested exception tree and calling Flatten() to produce a flat sequence. Console output shows the count of 4 and each caller-facing exception message in order.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Cuemon.Extensions;

namespace MyApp.Examples;

public static class ExceptionExtensionsExample
{
    public static void Demonstrate()
    {
        var exception = new InvalidOperationException(
            "First",
            new AmbiguousMatchException(
                "Second",
                new OutOfMemoryException(
                    "Third",
                    new AggregateException(new AccessViolationException("Fourth")))));

        IEnumerable<Exception> flattened = exception.Flatten();

        Console.WriteLine(flattened.Count());
        foreach (var ex in flattened)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

Methods

Name Description
Flatten(Exception)

Flattens any inner exceptions from the specified exception into an IEnumerable<T> sequence of exceptions.