【发布时间】:2021-01-18 23:27:13
【问题描述】:
我有一个应用程序在 try ... catch 块之外引发异常。异常如下所示:
2021-01-18 17:11:38.840 -06:00 [ERR] 集合已修改; 枚举操作可能无法执行。 2021-01-18 17:11:38.848 -06:00 [错误] 在 System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource 资源)在 System.Collections.Generic.List
1.Enumerator.MoveNextRare() at System.Collections.Generic.List1.Enumerator.MoveNext() 在 System.Windows.Forms.DataVisualization.Charting.ChartArea.GetPointsInterval(列表`1 seriesList, Boolean isLogarithmic, Double logarithmicBase, Boolean checkSameInterval, Boolean& sameInterval, Series& series) at System.Windows.Forms.DataVisualization.Charting.ChartArea.SetDefaultFromData(轴 轴)在 System.Windows.Forms.DataVisualization.Charting.ChartArea.SetDefaultFromIndexesOrData(轴 轴,轴类型轴类型)在 System.Windows.Forms.DataVisualization.Charting.ChartArea.SetDefaultAxesValues() 在 System.Windows.Forms.DataVisualization.Charting.ChartArea.SetData(布尔 initializeAxes,布尔 checkIndexedAligned) 在 System.Windows.Forms.DataVisualization.Charting.ChartArea.ReCalcInternal() 在 System.Windows.Forms.DataVisualization.Charting.ChartPicture.Paint(图形 图,布尔paintTopLevelElementOnly)在 System.Windows.Forms.DataVisualization.Charting.Chart.OnPaint(PaintEventArgs 吃 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 层)在 System.Windows.Forms.Control.WmPaint(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
我了解有一个集合在迭代时被修改。我似乎找不到它在哪里。我正在本地网络上的机器上进行远程调试。我已经包裹了我怀疑发生异常的区域, try ... catch 块但没有找到它。
我还在我的 Main() 中添加了以下内容以尝试捕获错误:
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
在 main 之后,我有以下处理程序:
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
// Log the exception, display it, etc
Console.WriteLine(e.Exception.Message);
Log.Error("");
Log.Error(e.Exception.Message);
Log.Error(e.Exception.StackTrace);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// Log the exception, display it, etc
Console.WriteLine((e.ExceptionObject as Exception).Message);
Log.Error("");
Log.Error((e.ExceptionObject as Exception).Message);
Log.Error((e.ExceptionObject as Exception).StackTrace);
}
如何获取引发异常的代码的行号?或者真的......我怎样才能在我的源代码中找到引发错误的地方?
【问题讨论】:
-
异常中断,然后将堆栈返回到您的代码。
-
似乎您可能需要锁定该容器,以防止在枚举期间进行修改。