diff --git a/src/Spectre.Console/Widgets/Exceptions/ExceptionFormatter.cs b/src/Spectre.Console/Widgets/Exceptions/ExceptionFormatter.cs index 0a946fe..42968c0 100644 --- a/src/Spectre.Console/Widgets/Exceptions/ExceptionFormatter.cs +++ b/src/Spectre.Console/Widgets/Exceptions/ExceptionFormatter.cs @@ -65,7 +65,7 @@ internal static class ExceptionFormatter var stackTrace = new StackTrace(ex, fNeedFileInfo: true); var allFrames = stackTrace.GetFrames(); - if (allFrames[0]?.GetMethod() == null) + if (allFrames.Length > 0 && allFrames[0]?.GetMethod() == null) { // if we can't easily get the method for the frame, then we are in AOT // fallback to using ToString method of each frame. diff --git a/src/Tests/Spectre.Console.Tests/Unit/AnsiConsoleTests.cs b/src/Tests/Spectre.Console.Tests/Unit/AnsiConsoleTests.cs index 4ef4163..95ff657 100644 --- a/src/Tests/Spectre.Console.Tests/Unit/AnsiConsoleTests.cs +++ b/src/Tests/Spectre.Console.Tests/Unit/AnsiConsoleTests.cs @@ -142,4 +142,21 @@ public partial class AnsiConsoleTests .ShouldBe("Hello\nWorld\n"); } } + + public sealed class WriteException + { + [Fact] + public void Should_Not_Throw_If_Exception_Has_No_StackTrace() + { + // Given + var console = new TestConsole(); + var exception = new InvalidOperationException("An exception."); + + // When + void When() => console.WriteException(exception); + + // Then + Should.NotThrow(When); + } + } }