Skip to Main Content
C# Cookbook, 2nd Edition
book

C# Cookbook, 2nd Edition

by Jay Hilyard, Stephen Teilhet
January 2006
Intermediate to advanced content levelIntermediate to advanced
1184 pages
43h 23m
English
O'Reilly Media, Inc.
Content preview from C# Cookbook, 2nd Edition
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# Cookbook

C# Cookbook

Joe Mayo
C# Cookbook

C# Cookbook

Stephen Teilhet, Jay Hilyard
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 0596100639Supplemental ContentCatalog PageErrata