Jump to content
  • Advertisement
Sign in to follow this  
andrew_480

C++ exception handling

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

How should I handle exceptions in c++? I used to use try catch throw exceptions, but after a while it seemed like overkill, so I went back to returning 0/NULL/false and using a report function (like :std::cerr << message;). I'm not really sure how to get the mix right.

Share this post


Link to post
Share on other sites
Advertisement
Use exceptions for 'exceptional cases', not just basic error handling. A good example would be if you failed to allocate memory (or worse, because you ran out..).

This topic came up a while ago and there were some real good tips in here Clicky (Jan W. and some of the moderators had some very good input). Its worth a red though to get an idea how may want to strategize.


HTH -

#dth-0

Share this post


Link to post
Share on other sites
Well you should hardly be using exceptions at all. Only when exceptional things go wrong. Now i admit that deciding whether something is exception or not is another matter and can be quite tricky. but in simple terms, the only exception you really have to worry about is std::bad_alloc thrown by new when it runs out of memory.

I would stick with returning error codes from functions and the like for nearly all error handling.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
Usually when people ask this sort of question it's because thier code is littered with catch blocks that are doing cleanup. The common answer is to remove the catch blocks entirely and use RIIA to do cleanup instead.

Share this post


Link to post
Share on other sites
Ok. So I should use a reporting function and return codes for smaller errors, and exceptions for the big things.

Small Errors:
File doesn't exist
Already initialized
Setting a variable

Big Things:
Setting video mode
Loading a file

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!