C# 聚合事件异常

C# 聚合事件异常,c#,exception,events,listener,aggregateexception,C#,Exception,Events,Listener,Aggregateexception,接下来我将学习一个教程,详细介绍如何处理一个事件的多个订阅服务器中抛出的事件。但是,当代码运行时,TargetInvocationExceptioncatch块没有捕获订阅服务器的方法体中抛出的异常:throw new Exception(“Hello”)和throw new Exception(“World”) 相反,我在第一个侦听器中得到了一个未处理的异常错误,可以预见,在private static void AlarmListener中的抛出新异常(“Hello”) 当我试图捕获被调用方

接下来我将学习一个教程,详细介绍如何处理一个事件的多个订阅服务器中抛出的事件。但是,当代码运行时,
TargetInvocationException
catch块没有捕获订阅服务器的方法体中抛出的异常:
throw new Exception(“Hello”)
throw new Exception(“World”)

相反,我在第一个侦听器中得到了一个未处理的异常错误,可以预见,在
private static void AlarmListener中的
抛出新异常(“Hello”)

当我试图捕获被调用方法的异常时,我做得不正确的是什么

class AggregatingExceptions
{              
    public void Main()
    {
        //create the alarm
        AlarmAndLocation alarm = new AlarmAndLocation();

        //subscribe the listeners 
        alarm.OnAlarmRaised += AlarmListener;
        alarm.OnAlarmRaised += AlarmListener2;

        try
        {
            alarm.RaiseAlarm("Kitchen");
        }
        catch (AggregateException agg)
        {
            foreach (Exception ex in agg.InnerExceptions)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

    private static void AlarmListener(object sender, AlarmEventArgs e)
    {
        Console.WriteLine("Alarm listener 1 called.");
        throw new Exception("Hello");
    }
    private static void AlarmListener2(object sender, AlarmEventArgs e)
    {
        Console.WriteLine("Alarm listener 2 called.");
        throw new Exception("World");
    }

}

public class AlarmAndLocation
{
   public event EventHandler<AlarmEventArgs> OnAlarmRaised = delegate { };

   public List<Exception> exceptionList = new List<Exception>();

    public void RaiseAlarm(string location)
    {            
        foreach (Delegate handler in OnAlarmRaised.GetInvocationList())
        {
            try
            {
                handler.DynamicInvoke(this, new AlarmEventArgs(location));
            }
            catch (TargetInvocationException ex) 
            {
                exceptionList.Add(ex.InnerException);
            }
        }

        if(exceptionList.Count > 0)
        {
            throw new AggregateException(exceptionList);
        }
    }
}

public class AlarmEventArgs : EventArgs
{
    public string  Location { get; set; }

    public AlarmEventArgs(string location)
    {
        Location = location;
    }
}
类聚合异常
{              
公共图书馆
{
//创建警报
AlarmAndLocation alarm=新的AlarmAndLocation();
//订阅听众
alarm.OnAlarmRised+=AlarmListener;
alarm.onAlarmRised+=AlarmListener2;
尝试
{
警报。升起武器(“厨房”);
}
捕获(聚合异常聚集)
{
foreach(agg.InnerExceptions中的异常ex)
{
控制台写入线(例如消息);
}
}
}
私有静态void AlarmListener(对象发送方,AlarmEventArgs e)
{
Console.WriteLine(“调用了报警侦听器1”);
抛出新异常(“Hello”);
}
私有静态无效AlarmListener2(对象发送方,AlarmEventArgs e)
{
WriteLine(“调用了报警侦听器2”);
抛出新异常(“世界”);
}
}
公共类AlarmAndLocation
{
public event EventHandler=delegate{};
公共列表例外列表=新列表();
公共void RaiseAlarm(字符串位置)
{            
foreach(onAlarmRised.GetInvocationList()中的委托处理程序)
{
尝试
{
DynamicInvoke(这个,新的AlarmEventArgs(位置));
}
捕获(目标异常)
{
异常列表.Add(例如InnerException);
}
}
如果(exceptionList.Count>0)
{
抛出新的AggregateException(exceptionList);
}
}
}
公共类AlarmEventArgs:EventArgs
{
公共字符串位置{get;set;}
公共AlarmEventArgs(字符串位置)
{
位置=位置;
}
}

也许我错了,但是
aggregateeexception
仅用于TPL@PavelAnikhouski如果是这样的话,我可以接受,但我希望
TargetInvocationException
catch块在出现意外行为之前仍会在
RaiseAlarm
内触发。目前,它没有到达任何
aggregateeexception
引用,因为它在动态调用中失败。