0

What kind of errors should I expect with a computer game written in C and how to handle them? With computer game I imply a program where there is no danger of any kind to human lives or "property".

I would like to add as few error handling code as necessary to keep everything as clear and simple as possible. For example I do not want to do this, because this is much simpler and sufficient for a game.

Up to now I have thought about this:

  • Error: out-of-memory when calling malloc.

    Handling: Print error message and call exit(EXIT_FAILURE); (like this)

  • Error: A programming error, i.e. something which would work if implemented correctly.

    Handling: Use assert to detect (which aborts the program if failed).

  • Error: Reading a corrupted critical file (e.g. game resource).

    Handling: Print error message and call exit(EXIT_FAILURE);

  • Error: Reading a corrupted non-critical file (e.g. load a saved game).

    Handling: Show message to user and ask to load another file.

Do you think this is a reasonable approach? What other error should I expect and what is a reasonable minimal approach to handle them?

Community
  • 1
  • 1
Danvil
  • 22,240
  • 19
  • 65
  • 88
  • It's simple, just write perfect code and then you don't need to handle errors! – nonsensickle Dec 25 '13 at 10:37
  • Out of memory (no more ram) and file write errors (no more disk space) could not be avoided even if my program would be written perfectly... – Danvil Dec 25 '13 at 10:40
  • 1
    1+ for starting to think about error handling prior to starting to code. – alk Dec 25 '13 at 10:47
  • Could you give me a quick comment on what is wrong with the question if you down-vote/close-vote? – Danvil Dec 25 '13 at 10:51
  • The downvotes are about "*Too broad*" and "*Mostly option based*". Concluding from this some guys do not seem to like conceptual questions on SO. Probably your question would have suite http://programmers.stackexchange.com/ better. – alk Dec 25 '13 at 11:01
  • Hmm, I dislike your question but not enough to downvote. Still, it's quite broad as it is asking for "best practice" advice. Stackoverflow asks you to try first and then ask a question. You're basically saying you haven't tried (although it is evident you did do research) and want some generic advice, which may be causing you trouble with those who skim read. Since it's marginal I'm leaving you be with a sarcastic comment. I believe that the given answers are a pretty good starting point but that you need to figure this out by coding it. – nonsensickle Dec 25 '13 at 11:02
  • Also, you might have better luck with downvotes on the http://programmers.stackexchange.com/. They seem more lenient. – nonsensickle Dec 25 '13 at 11:04
  • @nonsensickle: Thanks for the comment :) I already coded some smaller parts and have changed error handling two times now... – Danvil Dec 25 '13 at 11:05
  • "*... and have changed error handling two times now ...*" Learning is iterative process ... ;-) – alk Dec 25 '13 at 11:07
  • @alk: Oh well, but sharing wisdom does not hurt ;) – Danvil Dec 25 '13 at 12:37

3 Answers3

3

You can expect at least those errors to happen as mentioned in the documentation to the libraries you use. For a C program that typically is libc at least.

Check the ERRORS section of the man-pages for the functions you'd be using.


I'd also think this over:

I do not want to do this, because this is much simpler and sufficient for a game.

Imagine you'd have fought yourself through a dozen game-levels and then suddenly the screen is gone with an odd OOM*1-error message. And ... - you didn't save! DXXM!


*1 Out-Of-Memory

Community
  • 1
  • 1
alk
  • 69,737
  • 10
  • 105
  • 255
1

My suggestion is about:

  • turning on the compiler's flags for raising errors and warning, make your compiler as much pedantic as possible, -Wall, -Werror, -Wextra, for example are a good start for both clang and gcc
  • be sure that you know what undefined behaviour means and what are the scenarios that can possibly trigger an UB, the compiler doesn't always helps, even with all the warnings turned on.
  • make your program modular, especially when it comes to memory management and the use of malloc
  • be sure that your compiler and your standard library of choice both support the C standard that you pick
user2485710
  • 9,451
  • 13
  • 58
  • 102
1

As I've already stated in the comment I think this is a very broad question. However, it's Xmas and I'll try and be helpful (lest I upset Santa).

The general best practices have been given in the answers posted by @alk and @user2485710. I will try and give a generic boiler-plate for error handling as I see it in C.

You can't guard against everything without writing perfect code. Perfect code is unreachable (kind of like infinity in calculus) though you can try and get close.

If you try to put too much error handling code in, you will be affecting performance. So, let me define what I will call a simple function and a user function.

  • A user function is a function that can return an error value. e.g. fopen
  • A simple function is a function that can not return an error value. e.g.

    long add(int a, int b) 
    {
        long rv = a; // @alk - This way it shouldn't overflow. :P
    
        return rv + b;
    }
    

Here are a couple rules to follow:

  • All calls to user functions must handle the returned errors.
  • All calls to simple functions are assumed safe so no error handling is needed.
  • If a simple function's parameter is restricted (i.e. an int parameter that must be between 0 and 9) use an assert to ensure its validity (unless the value is the result of user input in which case you should either handle it or propagate it making this a user function).
  • If a user function's parameter is restricted and it doesn't cause an error do the same as above. Otherwise, propagate it without additional asserts.

Just like your malloc example you can wrap your user functions with code that will gracefully exit your game thereby turning them into simple functions.

This won't remove all errors but should help reduce them whilst keeping performance in mind. Testing should reduce the remaining errors to a minimum.

Forgive me for not being more specific, however, the question seems to ask for a generic method of error handling in C.

In conclusion I would add that testing, whether unit testing or otherwise, is where you make sure that your code works. Error handling isn't something you can plan for in its entirety because some possible errors will only be evident once you start to code (like a game not allowing you to move because you managed to get yourself stuck inside a wall which should be impossible but was allowed because of some strange explosive mechanics). However, testing can and should be planned for because that will reveal where you should spend more time handling errors.

Community
  • 1
  • 1
nonsensickle
  • 4,438
  • 2
  • 34
  • 61
  • Thanks a lot for the answer! Especially the difference between "user" and "simple" function is good to keep in mind. I thought that asking for error handling for "non-critical" programs would be a pretty strong constraint. For example in the sense: error which is due to a programming bug => screw it an crash instead of try to recover and keep it alive at all costs. – Danvil Dec 25 '13 at 12:44
  • Nitpicking: Your `add()` example could run into sort of error condition in case adding `int a` and `int b` would overlfow `int`. – alk Dec 25 '13 at 13:21
  • 1
    @alk, Damn, alright, challenge accepted! – nonsensickle Dec 25 '13 at 13:26