bug-commoncpp
[Top][All Lists]
Advanced

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

Common C++ persistence engine and exceptions


From: Pierre Bacquet
Subject: Common C++ persistence engine and exceptions
Date: Mon, 12 Aug 2002 14:43:08 +0200

Hello,

I've retrieved the last pre-2 versions (0.99.7 I think) and I've found
that
the persistence engine uses assert in some places.

Though the following:

void Engine::write(const std::string& str) THROWS (Engine::Exception)
{
  assert(myOperationalMode == modeWrite);
 ...

or

void Engine::read(std::string& str) THROWS (Engine::Exception)
{
  assert(myOperationalMode == modeRead);
  ...

may make sense (this is a kind of programmation error that shouldn't
happen during runtime)

I think that assert like :

  // Create the object (of the relevant type)
  object = TypeManager::createInstanceOf(className.c_str());
  if (object)
 {
   // Okay then - we can make this object
   myArchiveVector.push_back(object);
   std::string majik;
   read(majik);
   assert(majik == string("OBST"));
   object->read(*this);
   read(majik);
   assert(majik == string("OBEN"));
 }
  else
   THROW (Exception((std::string("Unable to instantiate object of class
")+className).c_str()));

could be replaced by exceptions in order to be handled, if needed, by
the calling program.
These kinds of problems (OBject STart and OBject ENd markers not found)
are likely to happen
if you read a persistence file produced by another program. So, this
kind of error is likely to
happen in regular run-time (i.e. not during development).

So, one may think about using :

if (object)
 {
   // Okay then - we can make this object
   myArchiveVector.push_back(object);
   std::string majik;
   read(majik);
   if (majik != string("OBST"))
     THROW (Exception((std::string("OBST not found
")+className).c_str()));
  object->read(*this);
   read(majik);
   if (majik != string("OBEN"));
     THROW (Exception((std::string("OBEN not found
")+className).c_str()));
 }
  else
   THROW (Exception((std::string("Unable to instantiate object of class
")+className).c_str()));

One may even think about displaying also the file position (
Engine::myUnderlyingStream.tellg() )
to help people understanding where things may have gone wrong.

What do you think of that ?

Greetings,

Pierre Bacquet.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]