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.
For a constructor with a single parameter, is it OK to throw an ArgumentNullException
Share
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)
alternatively