41

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?

1
  • 2
    If the supplied parameters are not valid, the method is expected to throw an ArgumentException. You can perfectly derive from it to use in such cases. Deriving from SystemException doesn't make sense but you throw an ArgumentNullException if an argument is null or ArgumentOutOfRangeException if it's outside the acceptable range. They both are derived from ArgumentException. So throwing a custom exception which is not derived from ArgumentException in such cases is IMHO something that you shouldn't do. So I'd go with Botz3000's answer. Commented Oct 12, 2012 at 11:23

5 Answers 5

41

The MSDN page about Best Practices for Handling Exceptions says

Throw an ArgumentException or a class derived from ArgumentException if invalid parameters are passed.

So i would say it's ok and even recommended.

0
7

One benefit of deriving from System.ArgumentException is that catch(System.ArgumentException) blocks will be able to handle your custom exception type as well as System.ArgumentException. This may or may not be what you want.

5

If you want to derive your "invalid argument" exceptions and they don't have a meaning beyond that, then ArgumentException sounds like a reasonable candidate:

ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the parameter specification of the called method. --MSDN

4

The original idea behind exceptions in .Net was that exceptions in the base class libraries (e.g. the System assemblies) would throw exceptions which derived from System.Exception and advised that all custom exceptions inherit from System.ApplicationException to differentiate between BCL exceptions and application exceptions however Microsoft have backtracked on this idea and now suggest that all exceptions inherit from System.Exception.

My advice is to inherit from the lowest Exception class in the framework that makes sense.

If your exception means something specific other than there is a problem with this argument, e.g. the type can add semantics to the exception (just as ArgumentNullException and ArgumentOutOfRangeException do) then make a custom one, if not then just use ArgumentException and supply a meaningful exception message.

2
  • User exceptions were supposed to derive from ApplicationException, not SystemException.
    – Nate C-K
    Commented Oct 16, 2012 at 20:17
  • Well spotted, I typed exception too often in that post, I'll correct it! Commented Oct 16, 2012 at 20:18
2

If you are 100% reusing the ArgumentException properties and add some extra functionality it, it would be ok. However when you only reuse it because of its name, it would not be ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.