Jump to content
  • Advertisement
Sign in to follow this  
Breaka

Error handling schemes

This topic is 5472 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
Games check for errors like any other program would. Program however you usually do, whether by checking return codes or catching exceptions.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!