64 lines
4.8 KiB
HTML
64 lines
4.8 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<title>Error codes - Boost.Outcome documentation</title>
|
|
<link rel="stylesheet" href="../css/boost.css" type="text/css">
|
|
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
|
|
|
|
<link rel="icon" href="../images/favicon.ico" type="image/ico"/>
|
|
<body><div class="spirit-nav">
|
|
<a accesskey="p" href="../motivation/errno.html"><img src="../images/prev.png" alt="Prev"></a>
|
|
<a accesskey="u" href="../motivation.html"><img src="../images/up.png" alt="Up"></a>
|
|
<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">
|
|
<div class="titlepage"><div><div><h1 style="clear: both">Error codes</h1></div></div></div>
|
|
|
|
|
|
<p>Error codes are reasonable error handling technique, also working in C.
|
|
In this case the information is also stored as an <code>int</code>, but returned by value,
|
|
which makes it possible to make functions pure (side-effect-free and referentially
|
|
transparent).</p>
|
|
<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">&</span> <span class="n">val</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="n">FILE</span><span class="o">*</span> <span class="n">fd</span><span class="p">;</span>
|
|
<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>
|
|
<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>
|
|
<span class="k">return</span> <span class="n">r</span><span class="p">;</span> <span class="c1">// return whatever error openFile() returned
|
|
</span><span class="c1"></span>
|
|
<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>
|
|
<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>
|
|
<span class="k">return</span> <span class="n">READERRC_NOINT</span><span class="p">;</span> <span class="c1">// my error code
|
|
</span><span class="c1"></span>
|
|
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="c1">// success
|
|
</span><span class="c1"></span><span class="p">}</span>
|
|
</code></pre></div>
|
|
<p>Because the type of the error information (<code>int</code>) is known statically, no memory
|
|
allocation or type erasure is required. This technique is very efficient.</p>
|
|
|
|
<h3 id="downsides">Downsides</h3>
|
|
|
|
<p>All failure paths written manually can be considered both an advantage and a
|
|
disadvantage. Forgetting to put a failure handling <code>if</code> causes bugs.</p>
|
|
|
|
<p>If I need to substitute an error code returned by lower-level function with mine
|
|
more appropriate at this level, the information about the original failure is
|
|
gone.</p>
|
|
|
|
<p>Also, all possible error codes invented by different programmers in different
|
|
third party libraries must fit into one <code>int</code> and not overlap with any other error
|
|
code value. This is quite impossible and does not scale well.</p>
|
|
|
|
<p>Because errors are communicated through returned values, we cannot use function’s
|
|
return type to return computed values. Computed values are written to function
|
|
<em>output</em> parameters, which requires objects to be created before we have values
|
|
to put into them. This requires many objects in unintended state to exist. Writing
|
|
to output parameters often requires an indirection and can incur some run-time cost.</p>
|
|
|
|
|
|
</div><p><small>Last revised: January 16, 2019 at 01:05:39 +0100</small></p>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="../motivation/errno.html"><img src="../images/prev.png" alt="Prev"></a>
|
|
<a accesskey="u" href="../motivation.html"><img src="../images/up.png" alt="Up"></a>
|
|
<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>
|
|
</html>
|