 | ExceptionExtensionsIsOneOf Method |
Checks if the exception is an instance of, sub-class of or implements one of a list of types.
Namespace: Capgemini.CommonObjectUtilsAssembly: Capgemini.CommonObjectUtils (in Capgemini.CommonObjectUtils.dll) Version: 1.3.0.0
Syntaxpublic static bool IsOneOf(
this Exception exception,
params Type[] argsRest
)
<ExtensionAttribute>
Public Shared Function IsOneOf (
exception As Exception,
ParamArray argsRest As Type()
) As Boolean
public:
[ExtensionAttribute]
static bool IsOneOf(
Exception^ exception,
... array<Type^>^ argsRest
)
static member IsOneOf :
exception : Exception *
argsRest : Type[] -> bool
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:
BooleanTrue 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)
{
}
catch(PathTooLongException exception)
{
}
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)))
{
}
else
{
throw;
}
}
See Also