Jump to content
  • Advertisement
Sign in to follow this  
mungulish

Exception handling and games

This topic is 4901 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
Advertisement
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
I believe it is good to check for stuff like NULL pointers. You can also use assert() to debug and turn it off once you've made the final version.

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
use exceptions as much as you want for initilaiztion but avoid having them in the game loop

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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!