Click or drag to resize
ExceptionExtensionsIsOneOf Method
Checks if the exception is an instance of, sub-class of or implements one of a list of types.

Namespace: Capgemini.CommonObjectUtils
Assembly: Capgemini.CommonObjectUtils (in Capgemini.CommonObjectUtils.dll) Version: 1.3.0.0
Syntax
public static bool IsOneOf(
	this Exception exception,
	params Type[] argsRest
)

Parameters

exception
Type: SystemException
The exception being checked.
argsRest
Type: SystemType
An array of types that the exception will be compared to.

Return Value

Type: Boolean
True if the exception's type matched any of the types.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type Exception. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Remarks
You can use this extension method to combine identical error handling code blocks by catching the common exception base class, testing it to see if it is one of the errors you want to handle and re-throwing it if it isn't.
Examples
Repeating error handling code like this can be combined.
try
{
    ...
}
catch(FileNotFoundException exception)
{
    // Handle error
}
catch(PathTooLongException exception)
{
    // Handle error
}
Instead we catch the common base exception (IOException), test it and handle or re-throw as appropriate.
try 
{
    ...
}
catch(IOException exception)
{
    if(exception.IsOneOf(typeof(FileNotFoundException), typeof(PathTooLongException)))
    {
        // Handle error
    }
    else
    {
        throw;
    }
}
See Also