tags:

views:

159

answers:

2

Hi, I am having a scenario, in which i have multiple delegates attached to the event (multicast delegate).

What could be the sequence of calling these methods/delegates (if any) ???

In case one of the method attached with the delegate throws the exception.

  1. Will the event stop processing further...???
  2. Will the rest of the methods attached continue...???

I am not clear on this how to handle this scenario.

Any help is highly appreciated.

+1  A: 

Yes, the event handler that throws the exception will stop executing further, and the exception will propogate up the call stack. This means that any subsequent event handlers will not get called. If you want to guarantee that each event handler gets called in the class that raises the event, then you'll need to iterate over each delegate within the MulticastDelegate and invoke it individually within a try-catch block. The easy solution, of course, is just catching any possible errors within your handlers, and I would say this is advisable in most cases.

Noldorin
A: 

Methods in multicast delegates get invoked in any order. (Generally, they get invoked in the order they were added to the delegate. But this is not always true.) Therefore, programmers should not depend on an invocation order.

You need to loop through all the methods in the multicast delegate using delegatename.GetInvocationList() and use a try catch block inside the loop to catch the exception and continue with the next method invocation.

See this article on how you can make sure rest of the methods are invoked inspite of exceptions in a few.

Rashmi Pandit