exceptions.html 5.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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>Exceptions - 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.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/errno.html"><img src="../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">Exceptions</h1></div></div></div>
  13. <p>Exceptions are the default mechanism in C++ for reporting, propagating and
  14. processing the information about function failures. Their main advantage is
  15. the ability to describe the &ldquo;success dependency&rdquo; between functions: if you want to
  16. say that calling function <code>g()</code> depends on the successful execution of function <code>f()</code>,
  17. you just put <code>g()</code> below <code>f()</code> and that&rsquo;s it:</p>
  18. <div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="kt">int</span> <span class="nf">a</span><span class="p">()</span>
  19. <span class="p">{</span>
  20. <span class="n">f</span><span class="p">();</span>
  21. <span class="n">g</span><span class="p">();</span> <span class="c1">// don&#39;t call g() and further if f() fails
  22. </span><span class="c1"></span> <span class="k">return</span> <span class="n">h</span><span class="p">();</span> <span class="c1">// don&#39;t call h() if g() fails
  23. </span><span class="c1"></span><span class="p">}</span>
  24. </code></pre></div>
  25. <p>In the C++ Standard terms this means that <code>f()</code> is <em>sequenced before</em> <code>g()</code>.
  26. This makes failure handling extremely easy: in a lot of cases you do not have
  27. to do anything.</p>
  28. <p>Also, while next operations are being canceled, the exception object containing
  29. the information about the initial failure is kept on the side. When at some point
  30. the cancellation cascade is stopped by an exception handler, the exception object
  31. can be inspected. It can contain arbitrarily big amount of data about the failure
  32. reason, including the entire call stack.</p>
  33. <h3 id="downsides">Downsides</h3>
  34. <p>There are two kinds of overheads caused by the exception handling mechanism. The
  35. first is connected with storing the exceptions on the side. Stack unwinding works
  36. independently in each thread of execution; each thread can be potentially handling
  37. a number of exceptions (even though only one exception can be active in one thread).
  38. This requires being prepared for storing an arbitrary number of exceptions of arbitrary
  39. types per thread. Additional things like jump tables also need to be stored in the
  40. program binaries.</p>
  41. <p>Second overhead is experienced when throwing an exception and trying to find the
  42. handler. Since nearly any function can throw an exception of any time, this is
  43. a dynamic memory allocation. The type of an exception is erased and a run-time type
  44. identification (RTTI) is required to asses the type of the active exception object.
  45. The worst case time required for matching exceptions against handlers cannot be easily
  46. predicted and therefore exceptions are not suitable for real-time or low-latency
  47. systems.</p>
  48. <p>Another problem connected with exceptions is that while they are good for program
  49. flows with linear &ldquo;success dependency&rdquo;, they become inconvenient in situations where
  50. this success dependency does not occur. One such notable example is releasing acquired
  51. resources which needs to be performed even if previous operations have failed.
  52. Another example is when some function <code>c()</code> depends on the success of at least one
  53. of two functions <code>a()</code> and <code>b()</code> (which try, for instance, to store user data by
  54. two different means), another example is when implementing a strong exception safety
  55. guarantee we may need to apply some fallback actions when previous operations have
  56. failed. When failures are reported by exceptions, the semantics of canceling all
  57. subsequent operations is a hindrance rather than help; these situations require special
  58. and non-trivial idioms to be employed.</p>
  59. <p>For these reasons in some projects using exceptions is forbidden. Compilers offer
  60. switches to disable exceptions altogether (they refuse to compile a <code>throw</code>, and turn
  61. already compiled <code>throw</code>s into calls to <code>std::abort()</code>).</p>
  62. </div><p><small>Last revised: March 22, 2019 at 13:58:05 -0700</small></p>
  63. <hr>
  64. <div class="spirit-nav">
  65. <a accesskey="p" href="../motivation.html"><img src="../images/prev.png" alt="Prev"></a>
  66. <a accesskey="u" href="../motivation.html"><img src="../images/up.png" alt="Up"></a>
  67. <a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="../motivation/errno.html"><img src="../images/next.png" alt="Next"></a></div></body>
  68. </html>