Misunderstood the question, see the comments.
You basically have three choices:
- Throw an exception
- Return an error code and use an out parameter to return the actual value
- Call an error callback
Exceptions: Very simple to implement, however they might incure a huge performance and/or memory impact, even during normal execution, depending on the compiler. You should run a performance analysis on your target compiler and platform, both in normal execution (i.e. don't actually throw an exception, use only a try/catch), and in exceptional execution. If your program slows down by a large amount after using exceptions, don't use them. However, Joel doesn't like exceptions because they create invisible return paths. Thanks to C++ RIAA (shared_ptr) I don't think that's an issue anymore, your mileage may vary.
Error codes: The upside of error codes are that you can see all code paths in your source code. There are however two problems with them:
- You need to check the error code of every call that can fail.
- They steal your return code, so you are forced to use an out parameter for your actual return value.
Another upside of this approach is that it is not memory intensive like some exception handling implementations. Also, you still need some CPU time to check the return code of your function calls, however this cost is usually only a fraction of the cost that exception handling incurs. Again, check with your compiler.
Callback: When something goes wrong, you call a callback. This only works if the callback is actually able to fix the problem (i.e. if the game can't allocate any more memory, defragment the heap).
Another approach are assertions, which are useful for testing for "impossible situations". They are usually only enabled if the game is running in a debug environment and are stripped out in a release environment. The assert implementation in assert.h does this automatically, it makes the assert macro do nothing if NDEBUG is defined.
I've taken much information for this answer from the book Game Engine Architecture by Jason Gregory, specifically Chapter 3.3.