Maha

Maha

  • NA
  • 0
  • 326.3k

NP82 catch() method

Feb 16 2008 11:01 AM

Hi Guys

 

NP82 catch() method

 

Program is coming under Exception Handling. Incomplete program is given. Students have to develop up the balance. Clue is given. One of the clue is catch(CarIsDeadException e). When executing completed program which is producing error result.

 

When I changed CarIsDeadException into Exception. That means catch(CarIsDeadException e) is now catch(Exception e). After the change program is executing well.       

  

I wish to know what should come in catch() method whether it is CarIsDeadException or Exception. Is it a printing mistake?    

 

Anyone knows please explain the reason.

 

Thank you

 

using System;

 

namespace SimpleException

{

   // C# allows nesting of classes, interfaces and structures.

   // Here, the car nests the radio.

   public class Car

   {

      // Internal state data.

      private int currSpeed;

      private int maxSpeed;

      private string petName;

 

      bool carIsDead;   // Is the car alive or dead?

 

      public Car(string name, int max, int curr)

      {

         currSpeed = curr;

         maxSpeed = max;

         petName = name;

         carIsDead = false;

      }

 

      // This custom exception describes the details of the car-is-dead condition.

      public class CarIsDeadException : System.Exception

      {

         // This custom exception maintains the name of the doomed car.

         private string carName;

         public CarIsDeadException() { }

         public CarIsDeadException(string carName)

         {

            this.carName = carName;

         }

         // Override the Exception.Message property.

         public override string Message

         {

            get

            {

               string msg = base.Message;

               if (carName != null)

                  msg += carName + " has bought the farm...";

               return msg;

            }

         }

      }

 

      // Throw the custom CarIsDeadException.

      public void SpeedUp(int delta)

      {

         // If the car is dead, just say so...

         if(carIsDead)

         {

            // Throw 'car is dead' exception.

            throw new CarIsDeadException(this.petName);

         }

         else // Not dead, speed up.

         {

            currSpeed += delta;

            if (currSpeed >= maxSpeed)

            {

               //Console.WriteLine("Sorry, {0} has overheated...", petName);

               carIsDead = true;

            }

            else

                  Console.WriteLine("=> CurrSpeed = {0}", currSpeed);

         }

      }

   }

   public class CarApp

   {

      // Speed up the car safely...

      public static int Main(string[] args)

      {

         // Make a car.

         Car buddha = new Car("Buddha", 100, 20);

         // Speed up past the car's max speed to

         // trigger the exception.

         try

         {

            for (int i = 0; i < 10; i++)

               buddha.SpeedUp(10);

         }

         catch (CarIsDeadException e)

         {

            Console.WriteLine("\nMethod: {0}", e.TargetSite);

            Console.WriteLine("\nMessage: {0}", e.Message);

         }

         // The error has been handled, continue on with the flow of this app...

         Console.WriteLine("\n***** Out of exception logic *****");

 

         return 0;

      }

   }

}


Answers (4)