I know this sort of code is not best practice, but nevertheless in certain situations I find it is a simpler solution:
if (obj.Foo is Xxxx)
{
// Do something
}
else if (obj.Foo is Yyyy)
{
// Do something
}
else
{
throw new Exception("Type " + obj.Foo.GetType() + " is not handled.");
}
Anyone know if there is a built-in exception I can throw in this case?
If
objis an argument to the method, you should throw anArgumentException:Otherwise, you should probably either throw an
InvalidOperationExceptionor create your own exception, like this: