[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Exception base class



Hi,

This is about something I found in PenguinPlay.h...
I added some comments in square brackets []


--------- PenguinPlay.h -------------
/**
  Exception Handling.  In the light of the fact that recent versions
  of gcc (egcs, 2.8 etc.) seem to have sane C++ support, we believe
  we can safely use this.

  We can even use RTTI if we want.  ?Should we?

[I don't think so. Especially with exceptions we should go back to the bare
minimum, in case the exception indicates a *real* problem (e.g. memory
allocation failure)]

**/

//
// Base exception class.
// Later we might feel the need to make this thing a bit cleverer.
//
class ppException{ 

  char* class_string;
 
[I'd suggest having three fields here (see my notes at the bottom for the
"const"):
  // Name of the class::method throwing the exception
  const char *EOrigin; 
  // Description of the exception class (see notes at bottom)
  const char *EType;
  // Description of the specific problem
  const char *Detail;
]

  public:

  ppException() : class_string("Error: Unknown PPlay Exception."){}

  //Every error of given type has a particular.
  /*virtual*/ char* ClassString(){ return class_string;}
  //this string should be set on a per construction basis.
  virtual char* Message()=0;

  //FIX we should perhaps have a "operator<<(ostream&)" function or the like
  //but the corporate will has not yet decided how we handle this.
  //(i.e. we haven't decided between iostream or stdio).
};
----------- End of PenguinPlay.h --------------

With such a class we'd have code like this:

class BillsEmpire
{
veryprivate:
  void CheckSituation (void)
  {
    if (Marketshare (MS) < Marketshare (Linux))
      throw (ppEImpossibleError ("BillsEmpire::CheckSituation",
                "We lost the Mono^H^H^H^HQuality lead!!!");
  }
};

int main (void)
{
  try
  {
    BillsEmpire::EliminateAllChoice ();
  }
  catch (ppException &Doomsday)
  {
    cout << Doomsday.Origin () << " reported ";
    cout << Doomsday.Type () << ". Details: ";
    cout << Doomsday.Detail () << endl;
  }
  return 0;
}

I'm using string constants intentionally because they don't need any memory
allocation/complex operations etc and thus lower the risk of screwing
things up even more by throwing an exception.

Comments?

Cu
	Christian

--

Where Do You Want To Swap Today?