这是我得到的一个例外。
我在调度程序UnhandledExceptionEventHandler
中捕获它在应用程序中.cs所以我现在不知道是什么行扔了它
关于如何跟踪和修复它的任何建议?没有内在的例外
e.Exception.GetBaseException().Message Object
reference not set to an instance of an object.
e.Exception.StackTrace
at System.Windows.Controls.ItemContainerGenerator.MoveToPosition(GeneratorPosition position, GeneratorDirection direction, Boolean allowStartAtRealizedItem, GeneratorState& state)
at System.Windows.Controls.ItemContainerGenerator.Generator..ctor(ItemContainerGenerator factory, GeneratorPosition position, GeneratorDirection direction, Boolean allowStartAtRealizedItem)
at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.StartAt(GeneratorPosition position, GeneratorDirection direction, Boolean allowStartAtRealizedItem)
at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
System.Diagnostics.Debug.WriteLine("App_DispatcherUnhandledException");
System.Diagnostics.Debug.WriteLine("(e == null) = " + (e == null).ToString());
System.Diagnostics.Debug.WriteLine("e.ToString() " + e.ToString());
System.Diagnostics.Debug.WriteLine("e.Exception.GetBaseException().Message " + e.Exception.GetBaseException().Message);
System.Diagnostics.Debug.WriteLine("e.Exception.GetBaseException().InnerException " + e.Exception.GetBaseException().InnerException);
System.Diagnostics.Debug.WriteLine("e.Exception.GetBaseException().Source " + e.Exception.GetBaseException().Source.ToString());
System.Diagnostics.Debug.WriteLine("e.Exception.StackTrace " + e.Exception.StackTrace.ToString());
System.Diagnostics.Debug.WriteLine("e.Exception.GetBaseException().StackTrace " + e.Exception.GetBaseException().StackTrace.ToString());
StringBuilder sb = new StringBuilder();
if (e.Exception.InnerException != null)
{
sb.AppendLine("InnerException");
sb.AppendLine(e.Exception.InnerException.Message);
if (!string.IsNullOrEmpty(e.Exception.InnerException.StackTrace))
{
int count = 0;
foreach (string line in e.Exception.InnerException.StackTrace.Split('n'))
{
sb.AppendLine(line.Trim());
count++;
if (count > 3) break;
}
}
}
sb.AppendLine("OuterException");
sb.AppendLine(e.Exception.Message);
if (!string.IsNullOrEmpty(e.Exception.StackTrace))
{
int count = 0;
foreach (string line in e.Exception.StackTrace.Split('n'))
{
sb.AppendLine(line.Trim());
count++;
if (count > 3) break;
}
}
MessageBox.Show(sb.ToString(), "App_DispatcherUnhandledException");
e.Handled = true;
if (MainWindow != null) MainWindow.Close();
}
当你调用一行代码时,比如
e.Exception.GetBaseException().Message
你必须首先确保 GetBaseException() 实际上返回了一个对象。
var baseException = e.Exception.GetBaseException();
if (baseException != null)
{
// Do something with baseException.Message
}
根据您的堆栈跟踪,这就是问题所在。 我对此持怀疑态度,因为 MSDN 文档指出GetBaseException()
应始终返回一个对象(如果您实际上正在处理尚未解除的实际异常)。