Sign in to follow this  

Exception handling and games

This topic is 4689 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

Do you use exception handling in games? I was thinking of using it. Would there be a significant performance impact? I read that it's not noticeable but is that true in practice? Or should I just avoid it, and compile my code with exception handling turned off?

Share this post


Link to post
Share on other sites
Exception handling is a very good way to respond efficiently to errors that depend on the environment the program runs in (received network packets, hardware/API interaction, file parsing). Whenever something should not fail, but might fail in rare occasions, use exceptions to propagate the error message until a state that can recover from it.

Share this post


Link to post
Share on other sites
Exception handling will have a big impact onj performance IF YOU USE IT IN THE WRONG WAY. I just take a silly example: throwing an exception to exit a loop will eat your processor time if the code is executed often.

Like its name imples, exception should be used to manage exceptionnal states. For example, a file thta should be here but which, for whatever reason, cannot be opened, or is corrupted. Or a communication problem between 2 machines.

Their goal is to make your code robust (see this thread for a (short) discussion about exception neutral/strong code) and should be used whenever the code has a chance to blow in an unexpected way.

Hope that I'm clear (looks like I'm not).

Regards,

Share this post


Link to post
Share on other sites
First, ASSERT combines a means of checking with a means of propagating an error (stopping the program). exceptions are only a means of propagating an error. An exception can not check if a pointer is NULL, it can only serve as a way of telling other parts of the program that the pointer is NULL.

The difference between uses of ASSERT and exceptions is that you use ASSERT to check a condition that you (as a programmer and software designer) a responsible for, and use a check with exception throwing for conditions that something else is responsible for.

Hopefully, once you release your software, you would have corrected all bugs inside it, so it is assumed that all conditions that your code should respect on its own will be verified, so ASSERTs can be removed as they no longer serve a purpose.

However, exception throwing remains useful for tracking down errors that you cannot prevent (those that are independent of your actions: the environment of the program, its output/input) and neutralizing them in a way smarter than the plain old "Bad input? Let's randomly crash.".

Share this post


Link to post
Share on other sites
Put it like this, if you're using the keyword 'new' then either use exceptions or let your program abrupty exit.
Note that 'new' does not return a null pointer so it is pointless to check for null - the reason for this is that if it did fail then the program would not even execute to this point.

This is a common mistake and there are numerous other cases that exceptions should be used. At the least you could enclose your entire code in an exception try and if one is detected then you can output the error to a file or something.

Share this post


Link to post
Share on other sites
New can be handled without exceptions. If you add (nothrow) after the new keyword then it will return 0 if it fails. I could even use the set_new_handler to take care of memory allocation failures. If I remember correctly it works with both forms of new.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kaze
use exceptions as much as you want for initilaiztion but avoid having them in the game loop


Why? If they are thrown, it's not as if performance would still be a priority anyway...

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by Kaze
use exceptions as much as you want for initilaiztion but avoid having them in the game loop


Why? If they are thrown, it's not as if performance would still be a priority anyway...


Indeed. From what I gather, only the catching code will affect code performance, so you may want to avoid CATCHING exceptions in your inner loops. Aka, write:

void render( void ) try
{
for_each( renderables.begin() , renderables.end() , render_obj() );
}
catch( ... )
{
}


instead of:

void render_obj::operator()( irenderable & r ) try
{
r.render();
}
catch( ... )
{
}


Assuming that irenderable s are low level primatives.

That said, even this optimization note is off the grapevine ( not tested by myself ), from having read a rant in which someone explained how they managed to get a 2x speed increase in code he was mantinencing by moving catch blocks into outer loops. This may be completely outdated info on my part, with modern compilers possibly being good enough to not need entry/leaving tracking info to be able to correctly handle exceptions.

Share this post


Link to post
Share on other sites

This topic is 4689 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this