首页 > 解决方案 > Application_DispatcherUnhandledException 问题

问题描述

private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show($"e.Exception?.Message}\n*********\n*e.Exception?.Data.ToString()}**********{e.Exception.StackTrace}");
         e.Handled = true;
}

我在 App.xaml.cs 中使用上述代码来捕获我的代码中未处理的任何错误。如果消息是:已添加具有相同键的项目。因为我试图在字典中插入一个现有的键,我怎么知道它是哪个字典?那是在 e 的某个地方吗?谢谢弗兰克

标签: c#exception-handlingapp.xaml

解决方案


简短的回答:你不会。

CLR 不在别处存储变量的名称。这里给出了解释:Getting the variable name for NullReferenceException

但是您可以检查e.data具有KeysValues属性的对象,因此您可以猜测哪个实例 cas 具有此值。

此外,您可以获得有关引发异常的源代码行的信息:如何获取导致异常的方法的名称


推荐阅读