using System;
class Test{
public static void Main(){
new Test().F();
}
event EventHandler ev1;
EventHandler ev2;
void F(){
ev1 += Handler1;
ev1 += Handler2;
ev1 += Handler3;
try{ev1(null,null);}catch(Exception ex){Console.WriteLine("catch["+ex.Message+"]");}
ev2 = chain(ev2, Handler1);
ev2 = chain(ev2, Handler2);
ev2 = chain(ev2, Handler3);
try{ev2(null,null);}catch(Exception ex){Console.WriteLine("catch["+ex.Message+"]");}
}
static EventHandler chain(EventHandler a, EventHandler b){
return (Object sender, EventArgs e) => {if(a!=null){a(sender, e);} if(b!=null){b(sender, e);}};
}
static void Handler1(Object sender, EventArgs e){
Console.WriteLine("1");
}
static void Handler2(Object sender, EventArgs e){
Console.WriteLine("2");
throw new Exception("ex2");
}
static void Handler3(Object sender, EventArgs e){
Console.WriteLine("3");
}
}
dXNpbmcgU3lzdGVtOwoKY2xhc3MgVGVzdHsKICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKXsKICAgICAgbmV3IFRlc3QoKS5GKCk7CiAgIH0KICAgCiAgIGV2ZW50IEV2ZW50SGFuZGxlciBldjE7CiAgIEV2ZW50SGFuZGxlciBldjI7CiAgIAogICB2b2lkIEYoKXsKICAgICAgZXYxICs9IEhhbmRsZXIxOwogICAgICBldjEgKz0gSGFuZGxlcjI7CiAgICAgIGV2MSArPSBIYW5kbGVyMzsKICAgICAgdHJ5e2V2MShudWxsLG51bGwpO31jYXRjaChFeGNlcHRpb24gZXgpe0NvbnNvbGUuV3JpdGVMaW5lKCJjYXRjaFsiK2V4Lk1lc3NhZ2UrIl0iKTt9CiAgICAgIAogICAgICBldjIgPSBjaGFpbihldjIsIEhhbmRsZXIxKTsKICAgICAgZXYyID0gY2hhaW4oZXYyLCBIYW5kbGVyMik7CiAgICAgIGV2MiA9IGNoYWluKGV2MiwgSGFuZGxlcjMpOwogICAgICB0cnl7ZXYyKG51bGwsbnVsbCk7fWNhdGNoKEV4Y2VwdGlvbiBleCl7Q29uc29sZS5Xcml0ZUxpbmUo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