C++ throw exceptions with arguments

Description

C++ throw exceptions with arguments

#include <iostream>
#include <string>
using namespace std;
class Measure/*from   w w  w.jav a 2 s.  co m*/
{
   private:
   int feet;
   float inches;
   public:
   class InchesEx              //exception class
   {
      public:
      string origin;       //for name of routine
      float iValue;        //for faulty inches value
      InchesEx(string or, float in)  //2-arg constructor
      {
         origin = or;      //store string
         iValue = in;      //store inches
      }
   };                      //end of exception class
   Measure()
   { feet = 0; inches = 0.0; }
   Measure(int ft, float in)
   {
      if(in >= 12.0)
         throw InchesEx("2-arg constructor", in);
      feet = ft;
      inches = in;
   }
   void getdist()              //get length from user
   {
      cout << "\nEnter feet: ";  cin >> feet;
      cout << "Enter inches: ";  cin >> inches;
      if(inches >= 12.0)
         throw InchesEx("getdist() function", inches);
    }
    void showdist()
      { cout << feet << "\'-" << inches << '\"'; }
};
int main()
{
      try
      {
         Measure dist1(17, 3.5);    //2-arg constructor
         Measure dist2;             //no-arg constructor
         dist2.getdist();            //get value
         s
         cout << "\ndist1 = ";  dist1.showdist();
         cout << "\ndist2 = ";  dist2.showdist();
      }
      catch(Measure::InchesEx ix)   //exception handler
      {
         cout << "\nInitialization error in " << ix.origin
         << ".\n   Inches value of " << ix.iValue
         << " is too large.";
      }
      cout << endl;
      return 0;
}



PreviousNext

Related