
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Handling Exceptions Individually for Each Delegate in a Multicast Delegate
|
493
thrown within one of the delegates. But once a delegate in a multicast delegate
throws an unhandled exception, no more delegates are fired. You need a way to trap
unhandled exceptions within each individual delegate while still allowing the rest of
the delegates to fire.
Solution
Use the GetInvocationList method as shown in Recipe 9.1. This method returns
each individual delegate from a multicast delegate and, by doing so, allows you to
invoke each delegate within the
try block of an exception handler.
The following delegate defines the
MyDelegate delegate type:
public delegate int MyDelegate( );
The method shown in Example 9-1 creates a multicast delegate called allInstances
and then uses GetInvocationList to retrieve each delegate individually. Each dele-
gate is then fired within the
try block of an exception handler.
Example 9-1. Handling exceptions individually for each delegate in a multicast delegate
public static void TestIndividualInvokesExceptions( )
{
MyDelegate myDelegateInstance1 = new MyDelegate(TestInvoke.Method1);
MyDelegate myDelegateInstance2 = new MyDelegate(TestInvoke.Method2);
MyDelegate myDelegateInstance3 = new MyDelegate(TestInvoke.Method3);
MyDelegate allInstances =
myDelegateInstance1 ...