I just wrote a dll library using vb.net. while testing, i got an error which i did not initially use a try-catch block. The code is

            'The first four bytes are for the Command
            _Command = DirectCast(BitConverter.ToInt32(data, 0), Commands)

            'The next four store the length of the name
            Dim nameLen As Integer = BitConverter.ToInt32(Data, 4)

            'The next four store the length of the message
            Dim msgLen As Integer = BitConverter.ToInt32(Data, 8)

            'This check makes sure that ClientName has been passed in the array of bytes
            If nameLen > 0 Then
                _ClientName = Encoding.UTF8.GetString(Data, 12, nameLen)
                _ClientName = Nothing
            End If

            'This checks for a null message field
            If msgLen > 0 Then
                _Message = Encoding.UTF8.GetString(data, 12 + nameLen, msgLen) <-- this is the error line
                _Message = Nothing
            End If
        Catch ex As Exception
        End Try

The error is

Index and count must refer to a location within the buffer. Parameter name: bytes

My question now is would it be considered good practice to throw errors from dll or return an error message to the client.

My client is expecting an array if bytes as response and i need a way to send back valid error message


Exceptions denote exceptional cases, which I think is in your case. Assuming I am understanding your question correctly, returning an empty/null result could also imply that the input was [4][4][4][0], so there was nothing to return, rather than an empty or malformed input array.

I would suggest that wrap that exception with an exception of your own, something which will make more sense to your client but without providing extra information on what is going on underneath. If your client does not care, and would like to treat situations like the above and situations where the input is invalid the same, then, it will be up to them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.