error_codes.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  3. <title>Error codes - Boost.Outcome documentation</title>
  4. <link rel="stylesheet" href="../css/boost.css" type="text/css">
  5. <meta name="generator" content="Hugo 0.52 with Boostdoc theme">
  6. <meta name="viewport" content="width=device-width,initial-scale=1.0"/>
  7. <link rel="icon" href="../images/favicon.ico" type="image/ico"/>
  8. <body><div class="spirit-nav">
  9. <a accesskey="p" href="../motivation/errno.html"><img src="../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../motivation.html"><img src="../images/up.png" alt="Up"></a>
  11. <a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="../motivation/std_error_code.html"><img src="../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">Error codes</h1></div></div></div>
  13. <p>Error codes are reasonable error handling technique, also working in C.
  14. In this case the information is also stored as an <code>int</code>, but returned by value,
  15. which makes it possible to make functions pure (side-effect-free and referentially
  16. transparent).</p>
  17. <div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="kt">int</span> <span class="nf">readInt</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">filename</span><span class="p">,</span> <span class="kt">int</span><span class="o">&amp;</span> <span class="n">val</span><span class="p">)</span>
  18. <span class="p">{</span>
  19. <span class="n">FILE</span><span class="o">*</span> <span class="n">fd</span><span class="p">;</span>
  20. <span class="kt">int</span> <span class="n">r</span> <span class="o">=</span> <span class="n">openFile</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="cm">/*out*/</span> <span class="n">fd</span><span class="p">);</span>
  21. <span class="k">if</span> <span class="p">(</span><span class="n">r</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
  22. <span class="k">return</span> <span class="n">r</span><span class="p">;</span> <span class="c1">// return whatever error openFile() returned
  23. </span><span class="c1"></span>
  24. <span class="n">r</span> <span class="o">=</span> <span class="n">readInt</span><span class="p">(</span><span class="n">fd</span><span class="p">,</span> <span class="cm">/*out*/</span> <span class="n">val</span><span class="p">);</span>
  25. <span class="k">if</span> <span class="p">(</span><span class="n">r</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
  26. <span class="k">return</span> <span class="n">READERRC_NOINT</span><span class="p">;</span> <span class="c1">// my error code
  27. </span><span class="c1"></span>
  28. <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="c1">// success
  29. </span><span class="c1"></span><span class="p">}</span>
  30. </code></pre></div>
  31. <p>Because the type of the error information (<code>int</code>) is known statically, no memory
  32. allocation or type erasure is required. This technique is very efficient.</p>
  33. <h3 id="downsides">Downsides</h3>
  34. <p>All failure paths written manually can be considered both an advantage and a
  35. disadvantage. Forgetting to put a failure handling <code>if</code> causes bugs.</p>
  36. <p>If I need to substitute an error code returned by lower-level function with mine
  37. more appropriate at this level, the information about the original failure is
  38. gone.</p>
  39. <p>Also, all possible error codes invented by different programmers in different
  40. third party libraries must fit into one <code>int</code> and not overlap with any other error
  41. code value. This is quite impossible and does not scale well.</p>
  42. <p>Because errors are communicated through returned values, we cannot use function&rsquo;s
  43. return type to return computed values. Computed values are written to function
  44. <em>output</em> parameters, which requires objects to be created before we have values
  45. to put into them. This requires many objects in unintended state to exist. Writing
  46. to output parameters often requires an indirection and can incur some run-time cost.</p>
  47. </div><p><small>Last revised: January 16, 2019 at 01:05:39 &#43;0100</small></p>
  48. <hr>
  49. <div class="spirit-nav">
  50. <a accesskey="p" href="../motivation/errno.html"><img src="../images/prev.png" alt="Prev"></a>
  51. <a accesskey="u" href="../motivation.html"><img src="../images/up.png" alt="Up"></a>
  52. <a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="../motivation/std_error_code.html"><img src="../images/next.png" alt="Next"></a></div></body>
  53. </html>