#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

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #891 – Example of Throwing a System Exception Type

  1. Pingback: Dew Drop – July 19, 2013 (#1,588) | Alvin Ashcraft's Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: