|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.ObjectModel;
using System.Reflection;
namespace System.Runtime
{
internal static class Fx
{
public static bool IsFatal(Exception exception)
{
while (exception != null)
{
if (exception is OutOfMemoryException && !(exception is InsufficientMemoryException))
{
return true;
}
// These exceptions aren't themselves fatal, but since the CLR uses them to wrap other exceptions,
// we want to check to see whether they've been used to wrap a fatal exception. If so, then they
// count as fatal.
if (exception is TypeInitializationException ||
exception is TargetInvocationException)
{
exception = exception.InnerException;
}
else if (exception is AggregateException aggException)
{
// AggregateExceptions have a collection of inner exceptions, which may themselves be other
// wrapping exceptions (including nested AggregateExceptions). Recursively walk this
// hierarchy. The (singular) InnerException is included in the collection.
ReadOnlyCollection<Exception> innerExceptions = aggException.InnerExceptions;
foreach (Exception innerException in innerExceptions)
{
if (IsFatal(innerException))
{
return true;
}
}
break;
}
else
{
break;
}
}
return false;
}
}
}
|