0

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

 Try
            '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)
            Else
                _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
            Else
                _Message = Nothing
            End If
        Catch ex As Exception
            Throw
        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

1 Answer 1

1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.