2

Possible Duplicate:
How to handle exception in multicast delegate in C#?

I am concerned about exceptions raised in event handlers of an event in C#. If we have an event A, and 3 listeners (A1,A2,A3), it's possible that an exception raised in one of the listeners will cause the other listeners never to treat the event. Eg: the invocation list for A is A1 then A2 then A3. If an exception is raised in A1, and not catched/treated in the event handler, it will "break" the invocation list and make A2 and A3 not to receive the event.

I know the correct way would be to treat the exceptions in the event handlers themselves and not allow exceptions to go all the way, but it's not a solution in my case: it's a legacy system and it would require a huge refactoring (more than 400 events, dozens of subscribers...).

Is there a generic way to invoke the events so that it goes through the invocation list and raises the event for all the listeners in a try catch? The examples I've found are not generic.

Any ideas / thoughts?

Community
  • 1
  • 1
srodriguez
  • 1,937
  • 2
  • 24
  • 42
  • 1
    What would you do if you caught an exception? How do you know that the exception hasn't damaged the application state in such a way that it would be bad idea to invoke the remaining listeners, or in such a way as to invalidate the results of calling the remaining listeners? Just let the exception propagate. – John Saunders Mar 19 '12 at 01:57

0 Answers0