Error handling schemes

Started by
7 comments, last by demonkoryu 19 years, 6 months ago
What Error handling mechanisms are most commonly used in the game programming genre? What error handling schemes do you guys use? Throwing and Catching exceptions? or logging errors to a file ? Or some other mechanism ?
Advertisement
duck
Exceptional C++ by Scott Meyers builds on GotW.... worth a look.
Also, have a look the "RAII" idiom, and be aware that goto is valid. (under some circumstances).
Shortly, any errors/exceptions should leave the object/function the error occured in in a consistent, valid state. If that isn't possible, it is a "fatal" error and the program must be halted.

Thermo
Quote:Original post by Konfusius
Exceptional C++ by Scott Meyers builds on GotW....

Actually it's Herb Sutter, not Scott Meyers. Meyers wrote Effective C++, etc. Sutter wrote Exceptional C++, etc.
Quote:Original post by Breaka
Throwing and Catching exceptions?


Yes.

Quote:or logging errors to a file ?

Yes.

Quote:Or some other mechanism ?

Yes. For example, goto.



with goto:
   x = new X(); if (!x) goto error_x;   y = new Y(x); if (!y) goto error_y;   z = new Z(y); if (!z) goto error_z;   return;   error_z:  y->detach( x ); delete y;   error_y:  x->cleanup(); delete x;   errror_x: throw std::runtime_error( "ARGH! SOMETHING WRONG!" );}


without goto:

   if ( x = new X() ) {      if ( y = new Y(x) ) {          if ( z = new Z(y) ) {              return;          }          else {              y->detach( x ); delete y;              x->cleanup(); delete x;              throw std::runtime_error( "ARGH!SOMETHING WRONG" );          }      }      else {          x->cleanup(); delete x;          throw std::runtime_error( "ARGH!SOMETHING WRONG" );      }   }   else {      throw std::runtime_error( "ARGH!SOMETHING WRONG" );   }}


Thermo
Quote:Original post by petewood
Quote:Original post by Konfusius
Exceptional C++ by Scott Meyers builds on GotW....

Actually it's Herb Sutter, not Scott Meyers. Meyers wrote Effective C++, etc. Sutter wrote Exceptional C++, etc.


OOps, sorry. I always mix them up. The portion of my brain which maintains their names seems to suffer from a serious amount of overlap :)

Thermo
Quote:Original post by Konfusius

if ( x = new X() ) {
if ( y = new Y(x) ) {
if ( z = new Z(y) ) {
return;
}
else {
y->detach( x ); delete y;
x->cleanup(); delete x;
throw std::runtime_error( "ARGH!SOMETHING WRONG" );
}
}
else {
x->cleanup(); delete x;
throw std::runtime_error( "ARGH!SOMETHING WRONG" );
}
}
else {
throw std::runtime_error( "ARGH!SOMETHING WRONG" );
}
}



If x is 0, wouldn't std::bac_alloc be thrown before std::runtime_error could be thrown?
Games check for errors like any other program would. Program however you usually do, whether by checking return codes or catching exceptions.
Quote:If x is 0, wouldn't std::bac_alloc be thrown before std::runtime_error could be thrown?


AFAIK only if the new failed because of memory shortage. If the memory could be allocated, but the constructor fails, the compiler will call operator delete, and it still could return 0 without throwing std::bad_alloc.

Thermo

This topic is closed to new replies.

Advertisement