#858 – Reading Exception Information in a Handler

When you catch an exception in a block of code that serves as an exception handler, you can read information about the error that occurred from an Exception object.  Every exception object will derive from the System.Exception class.  So, at a minimum, you’ll be able to access the properties of System.Exception.  (Not all properties will always contain values, however).

In the example below, the Bark method throws an exception of type System.Exception.  We query the System.Exception instance and dump out some of its values.

                try
                {
                    Dog d = new Dog("Kirby", 15);
                    d.Bark("Woof");
                }
                catch (System.Exception exc)
                {
                    Console.WriteLine(string.Format("Type = {0}", exc.GetType()));    // System.Exception
                    Console.WriteLine(string.Format("Message = {0}", exc.Message));   // Basic message
                    Console.WriteLine(string.Format("Source = {0}", exc.Source));     // Originating assembly
                    Console.WriteLine("Stack:");             // Dump out stack
                    Console.WriteLine(exc.StackTrace);
                }

Note that the call stack shows that Program.Main called Dog.Bark.
858-001

Advertisements

#390 – Using the Same Handler for Multiple Events

You can attach a single event handler (method) to more than one event, originating from more than one object.

In the example below, we have a general DogMadeSound handler that we attach to the Barked and Growled events of one Dog instance (Kirby) and to the Barked event of another Dog (Jack).  When any of these events fire, our handler will get called.

        static void Main()
        {
            Dog kirby = new Dog("Kirby");
            Dog jack = new Dog("Jack");

            // We handle Kirby's barks and growls and Jack's barks
            kirby.Barked += new EventHandler<DogEventArgs>(DogMadeSound);
            kirby.Growled += new EventHandler<DogEventArgs>(DogMadeSound);
            jack.Barked += new EventHandler<DogEventArgs>(DogMadeSound);

            kirby.Bark("Woof");
            kirby.Growl();
            jack.Bark("Yap");
        }

        static void DogMadeSound(object sender, DogEventArgs e)
        {
            Console.WriteLine("DogMadeSound handler: {0} - {1}", e.DogName, e.Sound);
        }

#370 – Subscribe to an Event by Adding an Event Handler

You subscribe to a particular event in C# by defining an event handler–code that will be called whenever the event occurs (is raised). You then attach your event handler to an event on a specific object, using the += operator.

Below is an example where we define an event handler for the Dog.Barked event.  Each time that Kirby barks, we’ll record the date and time of the bark in a list.

        private static List<DateTime> barkLog = new List<DateTime>();

        static void Main()
        {
            Dog kirby = new Dog("Kirby", 12);

            kirby.Barked += new EventHandler(kirby_Barked);

            kirby.Bark();
            Console.ReadLine();

            kirby.Bark();
            Console.ReadLine();
        }

        // Neither argument is used, for the moment
        static void kirby_Barked(object sender, EventArgs e)
        {
            // Log Kirby's barks
            barkLog.Add(DateTime.Now);
        }

Assuming that the Dog class fires its Barked event whenever we call the Bark method, our handler will get called whenever Kirby barks.