如何处理类型'System.ArgumentException'的未处理异常 [英] How to handle an unhandled exception of type 'System.ArgumentException'
问题描述
大家好,
我列出了C#中的所有USB应用程序并尝试使用脚踏板播放音频。
我收到以下错误。
mscorlib.dll中出现未处理的'System.ArgumentException'异常例外
附加信息:目标数组不够长,无法复制集合中的所有项目。检查数组索引和长度。
错误代码片段
Hi All,
I am listing all the USB applications in C# and trying to use foot pedal to play audio.
I am getting the below error.
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Destination array is not long enough to copy all the items in the collection. Check array index and length.
code snippet for error
private void ReadProcess(HidReport report)
{
byte[] message = report.Data;
uint _message;
Array.Reverse(message);
_message = BitConverter.ToUInt32(message, 0);
if (!once)
{
value = _message;
once = true;
}
推荐答案
不知道消息数组的长度,但ToUInt32很可能抛出异常。
Not knowing the length of the message array but ToUInt32 most likely throws the exception.
ArgumentException:
startIndex 是大于或等于值减去3的长度,并且小于或等于值的长度减去1。
ArgumentException:
startIndex is greater than or equal to the length of value minus 3, and is less than or equal to the length of value minus 1.
捕获并处理错误,您可以将操作包装在尝试...捕获 [ ^ ]阻止。
这篇关于如何处理类型'System.ArgumentException'的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!