• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
8<link rel="icon" href="../images/favicon.ico" type="image/ico"/>
9<body><div class="spirit-nav">
10<a accesskey="p" href="../motivation/errno.html"><img src="../images/prev.png" alt="Prev"></a>
11    <a accesskey="u" href="../motivation.html"><img src="../images/up.png" alt="Up"></a>
12    <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">
13  <div class="titlepage"><div><div><h1 style="clear: both">Error codes</h1></div></div></div>
14
15
16<p>Error codes are reasonable error handling technique, also working in C.
17In this case the information is also stored as an <code>int</code>, but returned by value,
18which makes it possible to make functions pure (side-effect-free and referentially
19transparent).</p>
20<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>
21<span class="p">{</span>
22  <span class="n">FILE</span><span class="o">*</span> <span class="n">fd</span><span class="p">;</span>
23  <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>
24  <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>
25    <span class="k">return</span> <span class="n">r</span><span class="p">;</span> <span class="c1">// return whatever error openFile() returned
26</span><span class="c1"></span>
27  <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>
28  <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>
29    <span class="k">return</span> <span class="n">READERRC_NOINT</span><span class="p">;</span> <span class="c1">// my error code
30</span><span class="c1"></span>
31  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>   <span class="c1">// success
32</span><span class="c1"></span><span class="p">}</span>
33</code></pre></div>
34<p>Because the type of the error information (<code>int</code>) is known statically, no memory
35allocation or type erasure is required. This technique is very efficient.</p>
36
37<h3 id="downsides">Downsides</h3>
38
39<p>All failure paths written manually can be considered both an advantage and a
40disadvantage. Forgetting to put a failure handling <code>if</code> causes bugs.</p>
41
42<p>If I need to substitute an error code returned by lower-level function with mine
43more appropriate at this level, the information about the original failure is
44gone.</p>
45
46<p>Also, all possible error codes invented by different programmers in different
47third party libraries must fit into one <code>int</code> and not overlap with any other error
48code value. This is quite impossible and does not scale well.</p>
49
50<p>Because errors are communicated through returned values, we cannot use function&rsquo;s
51return type to return computed values. Computed values are written to function
52<em>output</em> parameters, which requires objects to be created before we have values
53to put into them. This requires many objects in unintended state to exist. Writing
54to output parameters often requires an indirection and can incur some run-time cost.</p>
55
56
57        </div><p><small>Last revised: January 16, 2019 at 01:05:39 &#43;0100</small></p>
58<hr>
59<div class="spirit-nav">
60<a accesskey="p" href="../motivation/errno.html"><img src="../images/prev.png" alt="Prev"></a>
61    <a accesskey="u" href="../motivation.html"><img src="../images/up.png" alt="Up"></a>
62    <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>
63</html>
64