<PackageReference Include="NUnit" Version="4.3.1" />

ExceptionExtensions

static class ExceptionExtensions
using System; using System.Collections; using System.Runtime.CompilerServices; namespace NUnit.Framework { [NullableContext(1)] [Nullable(0)] internal static class ExceptionExtensions { [return: Nullable(2)] public static string GetStackTraceWithoutThrowing(this Exception exception) { if (exception != null) try { return exception.StackTrace; } catch (Exception ex) { return ex.GetType().Name + " was thrown by the Exception.StackTrace property."; } throw new ArgumentNullException("exception"); } public static string GetMessageWithoutThrowing(this Exception exception) { if (exception != null) try { return exception.Message; } catch (Exception ex) { return ex.GetType().Name + " was thrown by the Exception.Message property."; } throw new ArgumentNullException("exception"); } [return: Nullable(new byte[] { 0, 1 })] public static Result<IDictionary> GetDataWithoutThrowing(this Exception exception) { if (exception != null) try { return Result.Success(exception.Data); } catch (Exception ex) { return Result.Error<IDictionary>(ex.GetType().Name + " was thrown by the Exception.Data property."); } throw new ArgumentNullException("exception"); } } }