RaspberryIO_26/Swan.Lite/Extensions.Exceptions.cs

32 lines
2.0 KiB
C#
Raw Normal View History

2019-12-04 18:57:18 +01:00
using System;
using System.Linq;
using System.Threading;
2019-12-08 19:54:52 +01:00
namespace Swan {
/// <summary>
/// Provides extension methods for <see cref="Exception"/>.
/// </summary>
public static class ExceptionExtensions {
2019-12-04 18:57:18 +01:00
/// <summary>
2019-12-08 19:54:52 +01:00
/// Returns a value that tells whether an <see cref="Exception"/> is of a type that
/// we better not catch and ignore.
2019-12-04 18:57:18 +01:00
/// </summary>
2019-12-08 19:54:52 +01:00
/// <param name="this">The exception being thrown.</param>
/// <returns><see langword="true"/> if <paramref name="this"/> is a critical exception;
/// otherwise, <see langword="false"/>.</returns>
public static Boolean IsCriticalException(this Exception @this) => @this.IsCriticalExceptionCore() || (@this.InnerException?.IsCriticalException() ?? false) || @this is AggregateException aggregateException && aggregateException.InnerExceptions.Any(e => e.IsCriticalException());
/// <summary>
/// Returns a value that tells whether an <see cref="Exception"/> is of a type that
/// will likely cause application failure.
/// </summary>
/// <param name="this">The exception being thrown.</param>
/// <returns><see langword="true"/> if <paramref name="this"/> is a fatal exception;
/// otherwise, <see langword="false"/>.</returns>
public static Boolean IsFatalException(this Exception @this) => @this.IsFatalExceptionCore() || (@this.InnerException?.IsFatalException() ?? false) || @this is AggregateException aggregateException && aggregateException.InnerExceptions.Any(e => e.IsFatalException());
private static Boolean IsCriticalExceptionCore(this Exception @this) => IsFatalExceptionCore(@this) || @this is AppDomainUnloadedException || @this is BadImageFormatException || @this is CannotUnloadAppDomainException || @this is InvalidProgramException || @this is NullReferenceException;
private static Boolean IsFatalExceptionCore(this Exception @this) => @this is StackOverflowException || @this is OutOfMemoryException || @this is ThreadAbortException || @this is AccessViolationException;
}
2019-12-04 18:57:18 +01:00
}