2013-08-02 50 views
2

我们内部的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 ]“,所以我无法弄清楚发生了什么。有任何想法吗?

+0

你能抓住例外吗?向它添加更多数据并重新抛出它?我怀疑错误只发生在基于底层数据的特定记录中可能是空字段? – Luis

+0

例外有一个字典,你可以添加对象到它调整你的错误处理程序来报告这些 – Luis

回答

0

原因: 此错误的常见原因是您将无效值作为参数传递给您的函数。我的假设是,你在呼唤你的方法是这样的

ApplyCellStyleToEditingControl(getStyleFromSomeWhere); 

现在,在上面的例子中getStyleFromSomeWhere无效从而抛出异常

解决方案:

最好的解决方案是当你得到这个异常(并且你收到电子邮件)时,你也可以发送参数给你自己。通过这种方式,您可以诊断发生异常时的参数值,并诊断根本原因。

+0

嗨!感谢您的回应,但是如果您阅读我的问题,您将看到有问题的错误不会穿过我的代码。还有一个类似的例子,虽然这是通过我的代码,如果你看我的问题,你会看到我已经打印出来,它似乎是一个正常的字体,Microsoft Sans Serif,默认情况下与XP 。还有什么想法? – matao

相关问题