#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");
}