0

这是我得到的一个例外。

在 App.cs 中的 DispatcherUnhandledExceptionEventHandler 中捕获了它,所以我现在不知道哪条线正在抛出它
关于如何追踪并修复它的任何建议?没有内部异常

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();
}
4

1 回答 1

3

当你调用一行代码时

e.Exception.GetBaseException().Message

您必须首先确保 GetBaseException()实际上返回一个对象。

var baseException = e.Exception.GetBaseException();
if (baseException != null)
{
    // Do something with baseException.Message
} 

根据您的堆栈跟踪,这就是问题所在。我持怀疑态度,因为 MSDN 文档声明GetBaseException()应该始终返回一个对象(如果您实际上正在处理尚未解开的实际异常)。

于 2013-09-12T17:32:39.897 回答