Write Exceptions

Display formatted exception details with stack traces, colors, and clickable links

When you catch an exception and want to display it with formatting and colors, use WriteException.

Write a Basic Exception

To display an exception with default formatting, pass it to WriteException.

try
{
    ProcessUserData(null!);
}
catch (Exception ex)
{
    AnsiConsole.WriteException(ex);
}

Shorten File Paths

To make stack traces more readable, use ExceptionFormats.ShortenPaths.

try
{
    ProcessUserData(null!);
}
catch (Exception ex)
{
    AnsiConsole.WriteException(ex, ExceptionFormats.ShortenPaths);
}

For the cleanest output with clickable source links, combine ShortenEverything and ShowLinks.

try
{
    ProcessUserData(null!);
}
catch (Exception ex)
{
    AnsiConsole.WriteException(ex,
        ExceptionFormats.ShortenEverything | ExceptionFormats.ShowLinks);
}

Customize Colors

To match your application's theme, use ExceptionSettings with a custom ExceptionStyle.

try
{
    ProcessUserData(null!);
}
catch (Exception ex)
{
    AnsiConsole.WriteException(ex, new ExceptionSettings
    {
        Format = ExceptionFormats.ShortenEverything,
        Style = new ExceptionStyle
        {
            Exception = new Style(Color.Grey),
            Message = new Style(Color.White),
            Method = new Style(Color.Red),
            Path = new Style(Color.Yellow),
            LineNumber = new Style(Color.Blue),
        }
    });
}

See Also