Saturday, 3 September 2016

Is exception in one delegate will Stop execution of rest of the deligate?

Yes It stops execution of all rest delegates

How to handle it

Here is Problem :

public delegate void TheMulticastDelegate(int x,int y);
    class Program
    {
            private static void MultiCastDelMethod(int x, int y)
            {
                    try
                    {
                            int zero = 0;
                            int z = (x / y) / zero; 
                    }
                    catch (Exception ex)
                    {                               
                            throw ex;
                    }
            }

            private static void MultiCastDelMethod2(int x, int y)
            {
                    try
                    {
                            int z = x / y;
                            Console.WriteLine(z);
                    }
                    catch (Exception ex)
                    {
                            throw ex;
                    }
            }
            public static void Main(string[] args)
            {
                    TheMulticastDelegate multiCastDelegate = new TheMulticastDelegate(MultiCastDelMethod);
                    TheMulticastDelegate multiCastDelegate2 = new TheMulticastDelegate(MultiCastDelMethod2);

                    try
                    {
                            TheMulticastDelegate addition = multiCastDelegate + multiCastDelegate2;

                            foreach (TheMulticastDelegate multiCastDel in addition.GetInvocationList())
                            {
                                    multiCastDel(20, 30);
                            }
                    }
                    catch (Exception ex)
                    {
                            Console.WriteLine(ex.Message);
                    }

                    Console.ReadLine();
            }
    }


Here is Solution :

foreach (TheMulticastDelegate multiCastDel in addition.GetInvocationList())
{
    try
    {
        multiCastDel(20, 30);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

No comments:

Post a Comment