I need to check the type of the exception in a switch case, and perform different tasks for a specific exception, what I did for the moment is create a method that will be called when an exception is throw, so:
private static void ShuntException(Exception ex)
{
switch (ex.InnerException)
{
case System.Net.WebException:
DoSomething();
break;
}
}
essentially the problem's that I get this error:
Error : 'WebException' is a 'type', which is not valid in the given context
I can fix this situation using GetType().ToString()
and then writing the Exception
type manually like so:
private static void ShuntException(Exception ex)
{
switch (ex.InnerException.GetType().ToString())
{
case "System.Net.WebException":
DoSomething();
break;
}
}
the last example that I added working well, but I prefer the first that doesn't working for the error displayed by the compiler, what I missing or I doing wrong?
Thanks for any help.