我有一个类,它触发委托(delegate)给 DispatcherObject
目标的事件,该目标编码得很好,但我遇到的问题是,如果处理程序在其事件代码中抛出异常,我该怎么办?我不想阻止其他监听器处理该事件。我正在寻求有关如何处理此类问题的建议。
给定一个带有 Saved
事件的抽象基类,我的模式如下:
public event EventHandler<EventArgs> Saved;
public void Save() {
try {
OnSave();
} catch (Exception) {
// What should I do here? throwing prevents subsequent handlers,
// while catching gobbles up the exception. Should this be in OnSave()?
}
}
protected virtual void OnSave() {
EventHandler<EventArgs> evt = Saved;
if (evt != null) {
var args = EventArgs.Empty;
foreach (var handler in evt.GetInvocationList()) {
var target = handler.Target as DispatcherObject;
if (target == null || target.CheckAccess()) {
var h = handler as EventHandler<EventArgs>;
if (h != null) h(this, args);
} else {
target.Dispatcher.Invoke(handler, this, args);
}
}
}
}
我考虑过构建一个包含所有异常的异常,例如 ArrayException 或其他异常,但这似乎不对。
非常感谢有关在这里做什么的建议。
更新:我感谢 Daniel 和 Henrik 的回答,如果我可以将两者标记为已回答,我会的,我决定继续处理该事件,因为我真的不想要它完全不被注意,我的最终解决方案如下(对于其他寻找解决方案的人)。
public event EventHandler<EventArgs> Saved;
public void Save() {
OnSave();
}
protected virtual void OnSave() {
EventHandler<EventArgs> evt = Saved;
if (evt != null) {
var args = EventArgs.Empty;
var handlers = evt.GetInvocationList();
var exceptions = new Queue<Exception>(handlers.Length);
foreach (var handler in handlers) {
try {
var target = handler.Target as DispatcherObject;
if (target == null || target.CheckAccess()) {
var h = handler as EventHandler<EventArgs>;
if (h != null) h(this, args);
} else {
target.Dispatcher.Invoke(handler, this, args);
}
} catch (Exception ex) {
exceptions.Enqueue(ex);
}
}
if (exceptions.Count == 1) {
var ex = exceptions.Peek();
throw new Exception(ex.Message, ex);
}
if (exceptions.Count > 0) {
throw new AggregateException(exceptions);
}
}
}
最佳答案
无需创建自己的ArrayException
。您可以仅使用 System.AggregateException 来包装多个异常。
关于c# - 关于处理多线程事件中的异常的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6648379/