If I have a method that checks the validity of its arguments, is it ok to throw my own custom exceptions derived from System.ArgumentException
? I am asking because ArgumentException
is itself derived from System.SystemException
and I am seeing conflicting guidelines as to whether an application should derive from SystemException
. (Albeit indirectly, deriving from ArgumentException
is still tantamount to deriving from SystemException
.)
I see lots of guidelines saying don't derive from ApplicationException
, but derive from Exception instead. I'm happy with that. What I'm not sure about is whether it's ok to derive from SystemException too.
If I shouldn't derive from SystemException
, then what should I derive my "invalid argument"
exception classes from?
ArgumentException
. You can perfectly derive from it to use in such cases. Deriving fromSystemException
doesn't make sense but you throw anArgumentNullException
if an argument is null orArgumentOutOfRangeException
if it's outside the acceptable range. They both are derived fromArgumentException
. So throwing a custom exception which is not derived fromArgumentException
in such cases is IMHO something that you shouldn't do. So I'd go with Botz3000's answer.