I have an application, which resolves DNS hostnames to IP in one of its methods Question is, if the method which does this resolve is passed a null, what is the best thing to do? There are a few choices:
-Throw ArgumentNullException (programming errors should not be caught, except at the top, global error handler).
-Return another hostname (e.g. if a method accepts a person’s name as a parameter, and that parameter is null, I could return “person” instead). If this is not possible, ie there is only one host in the network for which I can provide a hostname for, then it would make sense to throw an exception. Perhaps better than using another hostname, which may annoy the end user.
What is better?
Thanks
Throw the exception. Your method should never return a seemingly valid value in a situation where it receives input that you consider unacceptable. It should either return null or throw an exception (the better route). The reason for this is even if you come up with some “error code string,” someone will not read your documentation or you will forget later and bang your head against the wall wondering why it’s not failing when it should be failing.