gpt4 book ai didi

wpf - UnhandledException 未被处理的可能原因是什么?

转载 作者:行者123 更新时间:2023-12-03 10:44:31 31 4
gpt4 key购买 nike

如题。

我一直在谷歌搜索并阅读 threads ...

我很确定没有其他地方可以使用 e.Handle 来阻止异常冒泡到 UnhandledException 处理程序。

我测试我的代码如下:

应用程序.xaml.cs

public partial class App : Application
{
private void Application_Startup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException +=
CurrentDomain_UnhandledException;
Application.Current.DispatcherUnhandledException +=
Current_DispatcherUnhandledException;
}
private void Current_DispatcherUnhandledException(
object sender,
DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.StackTrace);
e.Handled = true;
}

void CurrentDomain_UnhandledException(
object sender,
UnhandledExceptionEventArgs e)
{
MessageBox.Show(e.ExceptionObject);
}
}

绑定(bind)到 DataGrid 的 SelectedItem 的 ViewModel 属性:
private object _selectedItem;
public object SelectedItem
{
get
{
return this._selectedItem;
}
set
{
throw new Exception("Test");

if (this._selectedItem == value)
return;

this._selectedItem = value;

this.RaisePropertyChanged();
}
}

出于某种原因,在其他任何地方我都可以看到正在处理的异常,出现了消息框。但只有当我在DataGrid中选择时,触发SelectedItem中的setter,异常只显示在Visual Studio中,但没有被处理程序处理......是什么原因?

最佳答案

有时,如果您使用 TPL
即,如果我调用 BeginInvoke AppDomain.CurrentDomain.UnhandledException,Tasks Actions 和 Funcs 不会捕获它们的异常。

关于wpf - UnhandledException 未被处理的可能原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32988329/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com