O'Reilly logo

Pro C# 2010 and the .NET 4 Platform, Fifth Edition by Andrew Troelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.3. The Simplest Possible Example

To illustrate the usefulness of structured exception handling, we need to create a class that will throw an exception under the correct (or one might say exceptional) circumstances. Assume we have created a new C# Console Application project (named SimpleException) that defines two class types (Car and Radio) associated by the "has-a" relationship. The Radio type defines a single method that turns the radio's power on or off:

class Radio
{
  public void TurnOn(bool on)
  {
    if(on)
      Console.WriteLine("Jamming...");
    else
      Console.WriteLine("Quiet time...");
  }
}

In addition to leveraging the Radio class via containment/delegation, the Car class (shown below) is defined in such a way that if the user accelerates a Car ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required