#872 – Code After a throw Statement Is Not Executed

When you throw an exception using the throw statement, control transfers immediately to the calling function.  No additional statements in the method containing the throw statement are executed.

In the example below, if you call the Bark method and pass in a value for numTimes that is greater than 10, an exception is thrown.  In this case, the for loop will never execute–i.e. the dog won’t bark.

        // Dog.Bark
        public void Bark(int numTimes)
        {
            if (numTimes > 10)
                throw new ArgumentException(
                    string.Format("{0} is just too many times to bark", numTimes));

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

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

One Response to #872 – Code After a throw Statement Is Not Executed

  1. Pingback: Dew Drop – June 24, 2013 (#1,571) | 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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: