Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing ArgumentNullException in constructor?

For a constructor with a single parameter, is it OK to throw an ArgumentNullException inside the constructor if the parameter is null/empty? OR, should it be thrown in the method that actually uses the argument? Thanks.

like image 464
Ryan Peters Avatar asked Sep 02 '10 18:09

Ryan Peters


People also ask

When should you throw an ArgumentNullException?

The ArgumentNullException is thrown when a null value is passed to a method that does not accept null values as valid input. They're provided so that application code can differentiate between exceptions caused by null arguments and exceptions caused by arguments that are not null.

How do you handle 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 .

Should you throw NullReferenceException?

You should never throw a NullReferenceException manually. It should only ever be thrown by the framework itself. From the NullReferenceException documentation: Note that applications throw the ArgumentNullException exception rather than the NullReferenceException exception discussed here.

Can I throw exception from constructor C#?

Throwing exceptions in constructors in C# is fine, but a constructor should always create a valid object.

Can you throw an exception in a constructor?

Exceptions: Exceptions in Constructors. When throwing an exception in a constructor, clean up whatever objects and memory allocations you have made prior to throwing the exception, as explained in Exceptions: Throwing Exceptions from Your Own Functions.

How does the argumentnullexception constructor work?

This constructor initializes the Message property of the new instance to a system-supplied message that describes the error, such as "Value cannot be null." This message takes into account the current system culture. The following table shows the initial property values for an instance of ArgumentNullException.

When to throw an illegalargumentexception in constructor?

Here, if the id passed to the Animal object is null, we can throw NullPointerException For arguments that are non-null but still invalid, such as a negative value for age, we can throw an IllegalArgumentException. Security checks are another common use case for throwing exceptions in the constructor.

How do I throw an argumentnullexception in Java?

You should explicitly throw an ArgumentNullException if you are expecting the input to not be null. You might want to write a class called Guard that provides helper methods for this. So your code will be: void someMethod (SomeClass x, SomeClass y) { Guard.NotNull (x,"x","someMethod received a null x argument!");


1 Answers

Yes, if it is completely essential then throw the exception. You should not* throw the exception later.

Always remember the "Fail Early Principle". Concept being fail now, so you don't waste time debugging or experience unexpected system functionality.

Alternatively you could also throw a ArgumentException for "" and ArgumentNullException for null. In either case make sure you throw a valid Exception message.


Always a good reference article for managing exceptions: Good Exception Management Rules of Thumb


Side note on what @Steve Michelotti said (because i am a huge fan of CodeContracts)

Contract.Requires<ArgumentNullException>(inputParemeter!= null, "inputparameter cannot be null"); Contract.Requires<ArgumentException>(inputParemeter!= "", "inputparameter cannot be empty string"); 

alternatively

Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(inputParemeter), "inputparameter cannot be null or empty string"); 
like image 104
Nix Avatar answered Sep 28 '22 04:09

Nix