Should I make sure arguments aren't null before using them in a function.

Posted by Nathan W on Stack Overflow See other posts from Stack Overflow or by Nathan W
Published on 2008-10-13T22:30:58Z Indexed on 2010/03/12 2:17 UTC
Read the original article Hit count: 236

The title may not really explain what I'm really trying to get at, couldn't really think of a way to describe what I mean.

I was wondering if it is good practice to check the arguments that a function accepts for nulls or empty before using them. I have this function which just wraps some hash creation like so.

Public Shared Function GenerateHash(ByVal FilePath As IO.FileInfo) As String
        If (FilePath Is Nothing) Then
            Throw New ArgumentNullException("FilePath")
        End If

        Dim _sha As New Security.Cryptography.MD5CryptoServiceProvider
        Dim _Hash = Convert.ToBase64String(_sha.ComputeHash(New IO.FileStream(FilePath.FullName, IO.FileMode.Open, IO.FileAccess.Read)))
        Return _Hash
    End Function

As you can see I just takes a IO.Fileinfo as an argument, at the start of the function I am checking to make sure that it is not nothing.

I'm wondering is this good practice or should I just let it get to the actual hasher and then throw the exception because it is null.?

Thanks.

© Stack Overflow or respective owner

Related posts about exception

Related posts about functions