Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is always better to use error codes or exceptions. The choice between both depends on the implementation. I made a few example applications that explain in general what string errors, error codes and exceptions are.</p> <h2>String errors</h2> <p>This will work but it's ugly, also you don't know what errors there are defined without disecting the <code>GenerateError()</code> function).</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; class Example { public: std::string GenerateError(bool error) { if (error) { return "ERROR"; } return "OK"; }; }; int main() { Example example; if ("ERROR" == example.GenerateError(true)) { return 1; // failure } return 0; // success } </code></pre> <h2>Error Codes</h2> <p>With an <code>enum</code> you specify which error codes are available (in this case <code>Ok</code>, <code>Error</code> and <code>Error2</code>). This makes the code easier to understand and it will save you from mistakes. With enums you can also make use of <code>switch</code>.</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; enum ErrorCodes { Ok = 0, Error, Error2 //... }; class Example { public: ErrorCodes GenerateError(bool error) { if (error) { return ErrorCodes::Error; } return ErrorCodes::Ok; }; }; int main() { Example example; // Regular if statement if (ErrorCodes::Ok == example.GenerateError(true)) { return 1; // failure } else { return 0; // success } // switch statement switch (example.GenerateError(true)) { case Error: case Error2: return 1; // failure break; case Ok: return 0; // success break; } return 0; } </code></pre> <h2>Exceptions</h2> <p>Exceptions are a bit more complex but are definitely worth it to check out. Use exceptions when you want to obligate the user of the function to do something with the error. When the function does not require an action on an error it's probably better to use error codes.</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; class CustomException :public std::exception{ public: CustomException(const std::string m) :strMessage(m){} ~CustomException(void); const char* what(){ return strMessage.c_str(); } private: std::string strMessage; }; class Example { public: void GenerateError(bool error) { if (error) { throw CustomException("Critical error"); } return; }; }; int main() { Example example; try { example.GenerateError(true); } catch (CustomException ce) { std::cerr &lt;&lt; ce.what() &lt;&lt; std::endl; return 1; // failure } return 0; // success } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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