#888 – Objects Added to Exception’s Data Dictionary Must Be Serializable

You can use an Exception object’s Data property to add useful data to an exception that you are throwing.

When you add an entry to this dictionary, you need to make sure that the object being stored is serializable.  That is, it’s state can be completely captured and later used to reconstitute the object.

In the example below, we add a Dog instance to the Data property’s dictionary.  If the Dog class is not serializable, we’ll get an ArgumentException on the Add call.

        // Dog.Bark
        public void Bark(int numTimes)
        {
            if (numTimes > 12)
            {
                Exception xx = new Exception("Too much barking");
                xx.Data.Add("Dog", this);
                throw xx;
            }

            for (int i = 0; i < numTimes; i++)
                Console.WriteLine("Woof");
        }

We can fix this problem by marking the Dog class as serializable.

    [Serializable]
    public class Dog
Advertisements

#887 – Report Additional Data when You Throw an Exception

You can use the Data property of an Exception object to store some additional data that is meant to help the caller to better understand the context in which the exception occurred.

The Data property refers to a dictionary of keyword/value pairs.  You can add your own keyword/value pairs after you create the Exception object and before you throw the exception.

Both the key and the value can be any object, but you’ll most often use a string as the keyword.  In the code below, we use the Data property to pass some relevant data back to the caller.

        // Dog.Bark
        private int BarkInvokeCount = 0;
        public void Bark(int numTimes)
        {
            BarkInvokeCount++;

            if (numTimes > 12)
            {
                Exception xx = new Exception("Too much barking");
                xx.Data.Add("Dog", this);
                xx.Data.Add("NumTimesParameter", numTimes);
                xx.Data.Add("ExceptionTime", DateTime.Now);
                xx.Data.Add("BarkInvokeCount", BarkInvokeCount);

                throw xx;
            }

            Console.WriteLine(string.Format("{0}: Woof", Name));
        }

When we catch the exception, we have access to this information:
887-001

#459 – Assigning a Value of a Different Type to an enum

You can convert to an enum value from its underlying type by casting the underlying type (e.g. int) to the enum type.  You can also assign a value of a different type, one that does not match the underlying type, as long as the cast succeeds.

        // By default stored as int, with values 0,1,2,3
        public enum Mood { Crabby, Happy, Petulant, Elated };

        static void Main()
        {
            byte moodValue = 3;
            Mood mood;

            // Works (byte -> int)
            mood = (Mood)moodValue;

            // Also works, since cast converts value to 2 (Petulant)
            double moodValue2 = 2.1;
            mood = (Mood)moodValue2;
        }