Jump to content
  • Advertisement
Sign in to follow this  
CDProp

When to use exception handling? (C++)

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

Hello. I'm currently brushing up on exception handling. Most of the examples I'm reading, though they may be purposely trivial, seem like they could be more-easily handled by simply checking the return value of the function you call (assuming it returns success, otherwise some error code) and then going from there. So, I'm not quite sure.

Share this post


Link to post
Share on other sites
Advertisement
Exceptions are superior to manual error code techniques in cases where the exception should never occur in normal execution (i.e. if the error is exceptional behaviour). This allows you to avoid cluttering up the calling code with error checking logic and just assume that everything will always go as planned.

In the exceptional cases where an error does occur, the throwing/catching/unwinding process does incur a significant performance penalty (i.e. worse performance than manual error handling), but this is mitigated by the fact that in all of the non-exceptional cases you didn't pay the performance cost of the error-handling logic.

If a particular error will be a common occurrence (i.e. not exceptional behaviour), then just use traditional error handling techniques instead. The exception to this guideline is inside a constructor - the preferred error-handling mechanism for when a constructor fails is to throw an exception.

Share this post


Link to post
Share on other sites
Thank you for helping me. Don't you need some error-checking logic, anyway, in order to decide whether to throw the exception or not?

Share this post


Link to post
Share on other sites
Yes , but the *calling code* doesn't need to bother checking if an error occurred or not (e.g. by looking at return-codes, etc...). It can assume that everything is ok and continue executing, because the exception will basically goto your program back up to the correct catch statement in the case of an error.

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!