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!
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.
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.
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 .
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.
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”);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With