#891 – Example of Throwing a System Exception Type

When you throw an exception, you can thrown one of the existing system exception types.  You can also throw an ApplicationException or a custom exception type that you derive from Exception.

Below is an example of throwing one of the preexisting exception types.

        // Dog constructor
        public Dog(string name, int age)
        {
            // Make sure that age is within range
            if (age >= 30)
                throw new ArgumentOutOfRangeException(
                    "age",    // Parameter name
                    age,       // Invalid value
                    string.Format("Invalid age for {0}.  Must be < 30", name));
            Name = name;
            Age = age;
        }

891-001

Advertisements