Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class instance Error checking? 1

Status
Not open for further replies.

cdlvj

MIS
Nov 18, 2003
678
US
Is it not a good idea to always check and see if an class object successfully initiated?
I would think that it is possible that failure could occur.

class Image {
public:
int getWidth();
int getHeight();
void setX(int x);
int getX();
void setY(int y);
int getY();
void setRed(double red);
double getRed();
void setBlue(double blue);
double getBlue();
void setGreen(double green);
double getGreen();
private:
int _width;
int _height;
int _x;
int _y;
double _red[4000[4000;
double _blue[4000[4000;
double _green[4000[4000;
boolean isWithinSize(int s);
double clipIntensity(double brightness);
};


main()
{
Image *myimage = new Image;

if (!myimage)
printf("\nERROR ");
myimage->setY(4);

}
 
I think you're talking about whether the memory was successfully allocated by 'new'. In your example, if new fails your program will end because new throws a std::bad_alloc exception. You should do this:
Code:
int main()
{
   try
   {
      Image* myImage = new Image;
      myImage->setY( 4 );
   }
   catch (std::bad_alloc& e)
   {
      cout << "Memory allocation error!" << endl;
   }
   catch (...)
   {
      cout << "Caught an unknown exception!" << endl;
   }
}
 
thanks, as I think it should also be checked.

What about if you do it this way,

main()
{
Image myimage;

}

How do you check this? Or do you just get the Stack Overflow?
 
That should get allocated at load-time since it's not dynamically allocated, so if you don't have enough memory, your program won't even load.
 
More precisely: in the snippet above the Image class ctor invoked not in load time, it's a normal local context of the main() function. Try this:
Code:
class X {}; // Exception.

class Bad // Simulate constructor failure.
{
public:
  Bad() { throw X(); }
};

int main(int argc, char* argv[])
{
  try
  {
    Bad bad;
  }
  catch (...)
  {
    cout << "Sorry..." << endl;
  }
  cout << "Bye" << endl;
  return 1;
}
Remember that in C++ declarations are executable statements.
Only static storage duration object's ctors invoke before main() started.
May be it helps to check (some) objects initialization, but do not overcheck your code...
 
If a constructor would result in an object that is badly formed, it should nearly always throw an exception. This way, no "checks" are necessary. When you can handle failure (e.g. Well, it would be nice if I had that log file, but I don't actually need it..."), you explicitly handle it. Otherwise, you hopefully "handle" all unexpected exceptions by gracefully terminating your program or doing something otherwise sensible.

If throwing an exception is not possible or undesirable, the next-least-bad alternative is often to create a "zombie" object with a "valid" attribute that you check before doing anything with it. That leads to the strange circumstance where "invalid" is one of the valid states of an object. I'd avoid it if I could.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top