Throwing an exception vs checking null, for a null argument

What factors dictate throwing an exception if the argument is null (for example, if (a is null) throw new ArgumentNullException ()), as opposed to checking the argument if it is null.

I do not understand why it is necessary to throw an exception, and not check zero first? What is the use of an exception exception method?

This is for C # /. NET

thanks

+4
source share
5 answers

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.

+5
source

You usually throw an ArgumentNullException when an argument is passed that is null but should never be null. Its specific exception is ArgumentException for handling zeros.

Often, if you know that you will get null values ​​as arguments, you check the null value and plan the rest of your method accordingly - often create records if they are not there, or execute another routine, ve run if a valid argument is present. In this case, I usually do not use ArgumentNullException because I plan that null is valid.

I use ArgumentNullException when I'm sure that something should not be empty when it gets there, and I want to mark an argument that is defined as "invalid".

+8
source

Never write code that checks for a null argument and then does nothing. Hiding errors in the client code is pretty wrong, you want to be as useful as possible when your API is not used correctly.

ArgumentNullException is very useful.

+2
source

As a method writer, you must decide whether you can reasonably determine that the caller could mean null . If possible, take action. If you cannot, throw an ArgumentNullException to tell the caller that they gave you a value that you cannot use.

This defensive programming is to die as early as possible and as informatively as possible, instead of trying to limp along with a partially valid state that turns into much more subtle (and more complex corrections) errors.

+1
source

It's about how to contract for your method. If you want the contract to be that callers should not pass in null , you should throw an exception if you receive it. Do not trust the caller to never make this mistake! OTOH, if you want the contract not to be so restrictive in this argument (whatever that means: either it didn’t do anything gracefully and some other behavior), the exception is clearly wrong.

Whatever you do, document it . Give the poor caller a chance to get it right before throwing things on your method ...

0
source

Source: https://habr.com/ru/post/1305264/


All Articles