【问题标题】:What will be the possible reason for UnhandledException not being handled?UnhandledException 未被处理的可能原因是什么?
【发布时间】:2016-01-04 10:54:46
【问题描述】:

如标题。

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

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

我测试我的代码如下:

App.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);
    }
}

绑定到 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中,而没有被handlers处理......是什么原因?

【问题讨论】:

  • VS 被配置为在某些异常情况下中断。另外,我认为它不知道您在完全不同的地方捕获异常,因此它会将其视为未处理的异常。

标签: wpf mvvm datagrid exception-handling


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 2021-09-01
    • 2012-01-01
    • 2020-09-09
    • 2021-02-24
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多