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