Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArgumentException vs. ArgumentNullException?

Tags:

I’m refactoring some code and adding a method which will replace a (soon-to-be) deprecated method. The new method has the following signature:

FooResult Foo(FooArgs args) { ... } 

The deprecated method contained a growing list of parameters. These parameters are now properties on the FooArgs class. The deprecated method has several guard conditions which checked for null values with the following structure:

if (parameter1 == null)     throw new ArgumentNullException(“parameter1”); if (parameter... == null)     throw new ArgumentNullException(“parameter...”); if (parameterN == null)     throw new ArgumentNullException(“parameterN”); 

Now that the parameters have been collapsed into the FooArgs class should I throw an ArgumentNullException for the individual properties of the FooArgs parameter:

if (args.Property1 == null)     throw new ArgumentNullException(“args.Property1”); if (args.Property... == null)     throw new ArgumentNullException(“args.Property...”); if (args.PropertyN == null)     throw new ArgumentNullException(“args.PropertyN”); 

Or to throw a more general ArgumentException for the entire FooArgs parameter:

if (args.Property1 == null)     throw new ArgumentException(“Property1 cannot be null.”, “args”); if (args.Property... == null)     throw new ArgumentException(“Property... cannot be null.”, “args”); if (args.PropertyN == null)     throw new ArgumentException(“Property2 cannot be null.”, “args”); 

Thanks!

like image 473
John Laffoon Avatar asked Dec 10 '11 20:12

John Laffoon


People also ask

When should you throw an ArgumentException?

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. The ParamName property identifies the invalid argument.

What is ArgumentNullException in C#?

ArgumentNullException occurs when an invalid argument is passed to a method in C#. In this case, it refers to the passing of a null object when the method expects a non-null object or a value. Similar to other exceptions raised as a result of arguments, System.

How do you avoid ArgumentNullException?

To prevent the error, instantiate the object. An object returned from a method call is then passed as an argument to a second method, but the value of the original returned object is null . To prevent the error, check for a return value that is null and call the second method only if the return value is not null .

What is argument out of range exception?

An ArgumentOutOfRangeException exception is thrown when a method is invoked and at least one of the arguments passed to the method is not null and contains an invalid value that is not a member of the set of values expected for the argument.


2 Answers

You need to add a check for the args itself to be non-null. The ANE is not appropriate for individual components, so you need to use more general AE, like this:

if (args == null)     throw new ArgumentNullException(“args”); if (args.Property1 == null)     throw new ArgumentException(“Property1 cannot be null.”, “args”); if (args.Property... == null)     throw new ArgumentException(“Property... cannot be null.”, “args”); if (args.PropertyN == null)     throw new ArgumentException(“Property2 cannot be null.”, “args”); 
like image 54
Sergey Kalinichenko Avatar answered Oct 20 '22 01:10

Sergey Kalinichenko


While I agree completely with dasblinkenlight's answer, you may also want to consider moving the validation for FooArgs into the FooArgs class itself. If this class is designed specifically to move arguments around, it is likely not valid for it to have null proeprties, in which case, I would allow its constructor to do its validation.

like image 22
Reed Copsey Avatar answered Oct 19 '22 23:10

Reed Copsey