I want to be able to catch a WebFaultException<string> as the most specific then a WebFaultException<T> (T being any other type) as a more general case to handle. Is this possible?
try
{
// throw exception
}
catch (WebFaultException<string> e)
{
// handle more specific type
}
catch (WebFaultException<T> e)
{
// handle more general type
}
catch (Exception e)
{
}
As other answers have mentioned; you cannot directly specify to catch every
WebFaultException<T>without either knowing the specified type argument, or catching it’s base type instead.However, if you were wanting to catch all
WebFaultExceptionoccurrences and handle the response differently based on what the generic type is, then you could simply catch the base type and use reflection to determine the type of the generic argument usingType.GetGenericArguments(). Here is a simplecatchclause to show how you may go about doing it:If you want a more indepth example, I have uploaded a test program to demonstrate this to PasteBin. Feel free to take a look!