Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the appropriate exception to throw for an Object being passed in and needing to be cast to the generic parameter?

public IPredefinedInterface
{
    void DoSomething(Object obj);
}

public class MyClass<T> : IPredefinedInterface
{
    public void DoSomething(Object obj)
    {
        if (!(obj is T)) throw new ???

        SomeOtherFunc((T)obj);
    }
}

Not sure what the appropriate exception here is... ArgumentException, InvalidCastException, etc?

like image 780
michael Avatar asked Dec 04 '22 20:12

michael


1 Answers

It's a problem with the argument, so ArgumentException. You haven't actually done the cast, so InvalidCastException is not appropriate.

like image 118
John Saunders Avatar answered May 13 '23 02:05

John Saunders