Note that there are some explanatory texts on larger screens.

plurals
  1. POError Handling Paradigms: Mixing Exceptions and Error Codes
    primarykey
    data
    text
    <p>I'm currently developing a game engine in C++ using various pieces of middleware including OGRE (graphics), Bullet (physics), and OpenAL (sound), and I'm fairly early on in the project. It's at this point that I'm setting up my error handling mechanisms.</p> <p>I plan on exposing the engine to the user by containing it within a DLL and exporting a function which will return a pointer to the main engine object. This object will contain methods with which you can access various components of the engine - most objects will be accessed via interfaces so that the user is hidden from the actual implementations.</p> <p>I'm inclined to use error codes as my error reporting mechanism instead of exceptions since</p> <ol> <li>Passing exceptions through the DLL link increases the complexity of error reporting, forces me to export the exception classes, etc., and</li> <li>Error codes are generally more efficient, and for this reason they are common in real-time game engines.</li> </ol> <p>The only issue that arises then is one with constructors - they can't return error codes. I therefore plan on using error codes for all methods, but throwing exceptions when constructors fail. Since I plan on using the factory method pattern to generate objects and pass them to user code, the exceptions would be handled internally by the engine, and the user would just get a null pointer on construction failure. I realize that you usually shouldn't mix exceptions and return codes, but the alternatives don't sound any better:</p> <ul> <li>You could not do anything in the constructor and use some kind of init() method, but there goes RAII.</li> <li>You could set a flag and have some kind of isOk() or isInitialized() method, but now you've introduced the possibility of some zombie state where the object exists but failed to initialize, and you might forget to check to make sure it's alive.</li> </ul> <p>I'm aware of the inherent costs and benefits of both systems, and realize that it's generally a bad idea to mix the two. However, since constructors can't have some kind of return value, would it be unreasonable throw exceptions when constructors fail and use error codes elsewhere? Does anyone have a better suggestion?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload