RaspberryIO_26/Swan.Lite/Extensions.Exceptions.cs

50 lines
2.3 KiB
C#
Raw Normal View History

2019-12-04 18:57:18 +01:00
using System;
using System.Linq;
using System.Threading;
namespace Swan
{
/// <summary>
/// Provides extension methods for <see cref="Exception"/>.
/// </summary>
public static class ExceptionExtensions
{
/// <summary>
/// Returns a value that tells whether an <see cref="Exception"/> is of a type that
/// we better not catch and ignore.
/// </summary>
/// <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 bool 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 bool IsFatalException(this Exception @this)
=> @this.IsFatalExceptionCore()
|| (@this.InnerException?.IsFatalException() ?? false)
|| (@this is AggregateException aggregateException && aggregateException.InnerExceptions.Any(e => e.IsFatalException()));
private static bool IsCriticalExceptionCore(this Exception @this)
=> IsFatalExceptionCore(@this)
|| @this is AppDomainUnloadedException
|| @this is BadImageFormatException
|| @this is CannotUnloadAppDomainException
|| @this is InvalidProgramException
|| @this is NullReferenceException;
private static bool IsFatalExceptionCore(this Exception @this)
=> @this is StackOverflowException
|| @this is OutOfMemoryException
|| @this is ThreadAbortException
|| @this is AccessViolationException;
}
}