• Advertisement
Sign in to follow this  

Error handling

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

Hi, How are errors usually handled in game functions? I have just started with C++ to make some small games. Although I have learned a little C++ in the past, I'm a Java developer professionally, so am used to using exceptions. I see DirectX mostly uses the HRESULT error handling used by the COM api. Do your own functions usually handle errors in this way also, or do you normally use exceptions, introducing a mixture of methods which could cause confusion. If you use HRESULT, what is the best way to structure the return value so that you can return custom error results in a meaningful way?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by rakketh
How are errors usually handled in game functions?

I have just started with C++ to make some small games. Although I have learned a little C++ in the past, I'm a Java developer professionally, so am used to using exceptions.

I see DirectX mostly uses the HRESULT error handling used by the COM api. Do your own functions usually handle errors in this way also, or do you normally use exceptions, introducing a mixture of methods which could cause confusion.

If you use HRESULT, what is the best way to structure the return value so that you can return custom error results in a meaningful way?
I use exceptions almost exclusively for error handling. If a library (such as DirectX) uses return codes instead, I check them at the calling location, and throw an exception, including any relevant error details, and the error code - which unifies everything in the end. However, almost none of my exceptions are caught until they reach the outer level, and the program terminates - most of the errors from those APIs are pretty fatal to your program.

Share this post


Link to post
Share on other sites
Exception is generally used for fatal errors in C++ in which your program must exit. It's expensive to throw an exception in C++, so you don't use them in your collision detection code, for example.

For minor errors, such as failure in initializing sound system so your game would be played without sound, you should rely on function return values. Ideally, you'd want to have a static class or a singleton which is visible by all classes to log the error message. That way when a function fails, it can put a log in there why it fails and return, and the log persists.

I haven't used C++ in years, and that's what I used to do. Maybe other people have better solutions.

Share this post


Link to post
Share on other sites
Thanks for the quick replies. It sounds like you are both doing the same things with exceptions for fatal errors. To return minor errors, what kind of structure do you think best?

At the moment I have some enums in header files which are returned by the function whose header it is. For example, the result of an input could be SUCCESS, VALUE_TOO_BIG, NOT_A_NUMBER, etc. and the caller would look in inputHandler.h for the codes. Is it better to do it this way with the errors accompanying the class, or to have a general purpose error class with all of the values in?

Share this post


Link to post
Share on other sites
I personally don't use enums because I like to be fairly verbose. As long as you aren't throwing errors regularly, it is perfectly fine to output a string to a logfile with as much information as you want/need.

fprintf(logfile, "Oh no! Something bad!",GetError());

For fatal errors though, try/catch/throw can be easier to use.

Share this post


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

  • Advertisement