我们内部的WinForms应用程序中的一个未处理的异常处理程序定期报告此错误以及生产中使用的相同变体(我们将其配置为在发生未处理的异常时向我们发送电子邮件)。但是它不可复制,并且在不同的用户机器上以随机间隔发生,这些机器都是XP SP3。Windows窗体:“System.ArgumentException:参数无效。”来自系统堆栈
它似乎与数据网格中的蒙面文本框有关,但它似乎只出现在同一个三个控件中,在许多屏幕上出现在几十个之外。这些控件没有指定任何字体。
System.ArgumentException: Parameter is not valid.
at System.Drawing.Font.GetHeight(Graphics graphics)
at System.Drawing.Font.GetHeight()
at System.Drawing.Font.get_Height()
at System.Windows.Forms.Control.set_Font(Font value)
at System.Windows.Forms.DataGridViewComboBoxEditingControl.ApplyCellStyleToEditingControl(DataridViewCellStyle dataGridViewCellStyle)
at System.Windows.Forms.DataGridView.BeginEditInternal(Boolean selectAll)
at System.Windows.Forms.DataGridView.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WmKeyChar(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam,
IntPtr lparam)
真的有我难倒的事情是,堆栈跟踪是完全的系统命名空间中,所以有一些错误的字体,但我们不知道它是什么。 “参数无效”并没有提供很多有关参数无效的信息(这是来自底层的GDI库)。
我们也得到了类似的异常,通过我们的课程之一,这也使我捕获错误传播:
System.ArgumentException: Parameter is not valid.
at System.Drawing.Font.GetHeight(Graphics graphics)
at System.Drawing.Font.GetHeight()
at System.Drawing.Font.get_Height()
at System.Windows.Forms.Control.set_Font(Font value)
at MyApp.MaskedTextBoxEditingControl.ApplyCellStyleToEditingControl(DataGridViewCellStyle
dataGridViewCellStyle)
有问题的代码仅仅是这样的:
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle){
this.Font = dataGridViewCellStyle.Font;
// set other things
}
我将该行包装在try/catch块中,并在传入的Font上调用ToString(),并获得以下内容:“[Font:Name = Microsoft Sans Serif,Size = 8.25,Units = 3,GdiCharSet = 1,GdiVerticalFont = False ]“,所以我无法弄清楚发生了什么。有任何想法吗?
你能抓住例外吗?向它添加更多数据并重新抛出它?我怀疑错误只发生在基于底层数据的特定记录中可能是空字段? – Luis
例外有一个字典,你可以添加对象到它调整你的错误处理程序来报告这些 – Luis