ExceptionExtensions Class

Provides extension methods for exceptions.

public static class ExceptionExtensions

Methods

NameDescription
Exception.AnyCausingException(Func<Exception, bool>)Determines whether any of the causing exceptions for this exception satisfies the given predicate.
Exception.GetCausingExceptions()Enumerates all causing exceptions of the current exception.
Exception.IsCriticalError()Returns a value that tells whether an Exception is of a type that is better not caught, as it signals an unrecoverable condition.
Exception.Rethrow()Rethrows an exception, preserving the original stack trace. This method never returns.

Exception.AnyCausingException(Func<Exception, bool>)

Determines whether any of the causing exceptions for this exception satisfies the given predicate.

extension(Exception @this)
{
    public bool AnyCausingException(Func<Exception, bool> predicate)
}

Parameters

this Exception
The Exception on which this method is called.
predicate Func<Exception, bool>
A function to test each causing exception for a condition.

Returns

bool true if predicate returns true for at least one of the causing exceptions of this; otherwise, false.

Exceptions

ArgumentNullException
this is null. - or - predicate is null.

Remarks

For an explanation of what are considered causing exceptions of an exception, see the "Remarks" section of Exception.GetCausingExceptions.

This method yields the same result as calling exception.GetCausingExceptions().Any(predicate), but is faster and allocates less memory. It is used internally by the Exception.IsCriticalError method.

Exception.GetCausingExceptions()

Enumerates all causing exceptions of the current exception.

extension(Exception @this)
{
    public IEnumerable<Exception> GetCausingExceptions()
}

Parameters

this Exception
The Exception on which this method is called.

Returns

IEnumerable<Exception> An enumeration of the causing exceptions of this.

Exceptions

ArgumentNullException
this is null.

Remarks

The "causing exceptions" of an exceptions are the exceptions that caused it to be thrown, including itself (but excluding AggregateExceptions, as they are only meant as containers for other exceptions).

Enumerating the causing exceptions of an exception is useful, for example, when we want to reconstruct the chain of events that lead to the current exception being thrown. Exception.GetCausingExceptions enumerates innermost exceptions first, in order to make this chain of events clear.

Another common use case for causing exceptions is determining whether any of the causing exception of a caught exception is of a certain type, or anyway satisfies a given condition. While you can use LINQ's Enumerable.Any method on the return value of Exception.GetCausingExceptions, L.o.U.I.S. also provides the Exception.AnyCausingException extension method, which is optimized specifically for this case.

Exception.IsCriticalError()

Returns a value that tells whether an Exception is of a type that is better not caught, as it signals an unrecoverable condition.

extension(Exception @this)
{
    public bool IsCriticalError()
}

Parameters

this Exception
The Exception on which this method is called.

Returns

bool true if this represents a critical error; otherwise, false.

Exceptions

ArgumentNullException
this is null.

Remarks

The following exception types are considered critical errors:

Of course, not all exceptions of the above types must necessarily cause an application to terminate: for example, attempting to dynamically load an assembly as a plug-in will cause a BadImageFormatException to be thrown if the assembly file is corrupted. However, these situations should be handled explicitly, by catching specific exception types in restricted contexts (to continue the above example, a catch (BadImageFormatException) block should be present in the method that attempts to load the plug-in) and appropriate action (such as alerting the user, logging, etc.) should be taken instead of letting the exception "fall through" to external catch blocks.

This method is mainly meant for "generic" catch blocks, when the only valuable information is that "something has gone wrong". In these cases, a simple catch or catch (Exception ex) block will trigger warning CA1031 unless the exception is rethrown, because a potential serious problem is being hidden from the user; a conditional catch (Exception ex) when (!ex.IsCriticalError()), however, will not only prevent the warning, but correctly avoid hiding unrecoverable error conditions.

Exception.Rethrow()

Rethrows an exception, preserving the original stack trace. This method never returns.

extension(Exception @this)
{
    public void Rethrow()
}

Parameters

this Exception
The Exception on which this method is called.

Exceptions

ArgumentNullException
this is null.