框架在ieditableobject . enddit中捕获异常

本文关键字:捕获异常 enddit ieditableobject 框架 | 更新日期: 2023-09-27 18:17:54

如果在IEditableObject中抛出异常。结束(当使用绑定从WPF数据网格设置时)。

异常被框架捕获,该消息显示在控制台中"A first chance exception of type 'System"。得到NullReferenceException。"。

我实际上希望这个异常更明确,因为我"处理"所有未处理的异常使用DispatcherUnhandledException和AppDomain.CurrentDomain.UnhandledException。在那里我记录异常并在关闭应用程序之前通知用户。

我是否需要使用某种hack,比如为每个EndEdit添加try-catch并使用Application重新抛出异常。调度员?

框架在ieditableobject . enddit中捕获异常

下面是我最后做的

public class Editable : NotificationObject, IEditableObject
{
...
    public void EndEdit()
    {
            try
            {
                ...
            }
            catch (Exception e)
            {
                ExceptionUtils.ThrowOnUIThread(e);
            }
        }
    }
}

public static class ExceptionUtils
{
    public static void ThrowOnUIThread(Exception exception)
    {
        exception.PreserveStackTrace();
        Application.Current.Dispatcher.BeginInvoke(new Action(() => { throw exception; }));
    }
}

关于PreserveStackTrace请参见此问题