Your method can do one of three things when the argument is zero. He can throw an exception, he can return without any action, or he can make assumptions and try to continue. I assume that you are trying to choose between the first two options.
Since the calling method can always check the arguments before calling your method, it can prevent the transfer of invalid values. This is true no matter how your method handles invalid values.
When your method is called with invalid arguments, it must notify the caller that processing does not continue. You can report this by throwing an exception or returning the error value. If you check the null value and do not return the error value, the calling method will consider your method processed without errors.
How do you want the calling method to handle the case when your method is not being processed? If passing null is common and should be easily handled by the calling method, returning an error with a null argument is acceptable. In this case, the calling method either checks the previous arguments or the return value after selection to those who write the calling method.
If passing a null value is very rare, throw an exception. The calling method can prevent the exception by checking the arguments before calling the method, as described above. Throwing an exception, the call stack can be unwound without the need to add a lot of code.
Summary:
If the argument set to null is normal and the invocation method must be written to handle the returned function without doing anything, just return the error value. If a null argument is rare, and you cannot expect the calling method to process your method without doing anything, throwing an exception, so the call stack is unwound.
source share