For example if you pass a car into a sub that makes use of a car who should be checking for null references?
Should I even bother wrapping this code in the below if statement? It seems quite redundant, obviously I could have given a better example because I can’t catch the exception and handle it in any way.
Or should I just let the exception bubble up to the caller?
For example:
Public Sub CarService(ByVal car As ICar)
If car IsNot Nothing Then
'do logic here
Else : Throw New ArgumentNullException
End If
End Sub
Throwing a
NullReferenceExceptionyourself is actually pointless. As this isaccess the same reference that you manually check for
null) andhere. For a related discussion see Is it necessary to throw a NullReferenceException from an extension method?.
You should however, arguably at least for the public API surface, check for null references and throw an
ArgumentNullException.Or look into Code Contracts.