When is a null pointer / reference exception handled preferable for performing a null check?

I have a strange question that I have always thought about, but never seen in practical use. I am looking for justification to be enough.

When is a null pointer / reference exception handled preferable for performing a null check? If at all.

This applies to any language that needs to deal with null pointers / references that have exception handling functions.

My usual answer to this would be to do a null check before doing anything with a pointer / reference. If not null, continue as usual and use it. If null, handle the error or raise it.

ie, (in C #)

string str = null; if (str == null) { // error! } else { // do stuff int length = str.Length; // ... } 

However, if we do not check and just blindly use it, an exception will be thrown.

 string str = null; int length = str.Length; // oops, NullReferenceException // ... 

As an exception, we could probably catch him, so nothing prevents us from doing this (or is there?):

 string str = null; try { int length = str.Length; // oops, NullReferenceException // ... } catch (NullReferenceException ex) { // but that ok, we can handle it now } 

Now I admit that this is not the cleanest code, but it is no less working code, and I would not do it normally. But is there a design template or something where this is useful? Perhaps more useful than doing a direct, zero check in advance.

The only cases I can imagine that this can be useful are a multi-threaded environment in which an unprotected shared variable gets too much value. But how often does this happen? Good code that protects variables will not have this problem. Or, perhaps, if someone wrote a debugger and wanted the exception to be thrown open only for wrapping it or something else. Maybe an invisible performance advantage, or eliminates the need for other things in the code?

I may have answered some of my questions, but are there any other possibilities for this? I'm not looking for โ€œdo it just because we canโ€ examples or just poorly written code, but practical use for it. Although I will be fine, "there is no practical use for this, check."

+2
source share
5 answers

The problem is that all null pointer exceptions look the same. Any accounting that can be added to indicate which name the exception has raised cannot be more efficient than just checking for a null value.

+3
source

If you expect that the value will not be NULL, then there is no point in doing any checks. But when accepting arguments, for example, it makes sense to test the arguments you require with non-zero, and call ArgumentNullExcept if necessary.

This is preferable for two reasons:

  • Usually you use the single-line form of the ArgumentNullException constructor, passing the argument name. This provides very useful information during debugging, as now the coding person knows which argument was null. If your source is not available to them (or if the exception trace was sent by the end user without the debugging symbols installed), otherwise it would be impossible to say what really happened. With other types of exceptions, you can also indicate which variable was null, and this can be very useful information.
  • Capturing null dereferencing is one of the most expensive operations the CLR can perform, and this can have a serious performance impact if your code throws a lot of NullReferenceExceptions. Testing for invalidation and doing something else (even throwing an exception!) Is a cheaper operation. (A similar principle is that if you declare a catch block with the explicit intention of catching NRE, you are doing something wrong elsewhere, and you should fix it there instead of using try / catch. Catching NREs should never be an integral part of any algorithm .)

Also, do not put your code in null tests in places where you never expect null values. But if there is a possibility that the reference variable may be null (for example, if the link is provided by external code), you should always check.

+1
source

I have been programming Java for more than 10 years, and I cannot recall a single time when I explicitly tried to catch the null pointer exception.

Any variable is expected to sometimes be null (for example, an optional attribute of an object), in this case I need to check for null (obj.attr! = Null) or I expect the variable to not be zero (in this case, zero indicates an error elsewhere in the code). If I write a method (say, isUpperCase) whose argument (String s) is null, I explicitly check for null and then throw an IllegalArgumentException.

What I never did is quietly recovering from zero. If a method should return the number of uppercase characters in a string and the argument passed was null, I would never have โ€œhiddenโ€ it by silently returning 0 and thus masking a potential error.

+1
source

Personally (doing C ++), if there is no STRONG contract that assures me that my pointers are not nullptr, I always check them and return an error or silently return if it is allowed.

0
source

In Objective-C, you can safely do anything from scratch, which you can do with non-nil and nil is noop. It turns out to be surprisingly convenient. Much less validation of patterns, and exceptions are reserved for truly exceptional circumstances. Or will it be considered sloppy coding?

0
source

All Articles