error_handling.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
  2. <html>
  3. <head>
  4. <meta name="generator" content=
  5. "HTML Tidy for Cygwin (vers 1st April 2002), see www.w3.org">
  6. <meta http-equiv="Content-Type" content=
  7. "text/html; charset=windows-1252">
  8. <title>Error and Exception Handling</title>
  9. </head>
  10. <body>
  11. <h1>Error and Exception Handling</h1>
  12. <h2>References</h2>
  13. <p>The following paper is a good introduction to some of the issues of
  14. writing robust generic components:</p>
  15. <blockquote>
  16. <a href="generic_exception_safety.html">D. Abrahams: ``Exception Safety
  17. in Generic Components''</a>, originally published in <a href=
  18. "http://www.springer.de/cgi-bin/search_book.pl?isbn=3-540-41090-2">M.
  19. Jazayeri, R. Loos, D. Musser (eds.): Generic Programming, Proc. of a
  20. Dagstuhl Seminar, Lecture Notes on Computer Science 1766</a>
  21. </blockquote>
  22. <h2>Guidelines</h2>
  23. <h3>When should I use exceptions?</h3>
  24. <p>The simple answer is: ``whenever the semantic and performance
  25. characteristics of exceptions are appropriate.''</p>
  26. <p>An oft-cited guideline is to ask yourself the question ``is this an
  27. exceptional (or unexpected) situation?'' This guideline has an attractive
  28. ring to it, but is usually a mistake. The problem is that one person's
  29. ``exceptional'' is another's ``expected'': when you really look at the
  30. terms carefully, the distinction evaporates and you're left with no
  31. guideline. After all, if you check for an error condition, then in some
  32. sense you expect it to happen, or the check is wasted code.</p>
  33. <p>A more appropriate question to ask is: ``do we want stack unwinding
  34. here?'' Because actually handling an exception is likely to be
  35. significantly slower than executing mainline code, you should also ask:
  36. ``Can I afford stack unwinding here?'' For example, a desktop application
  37. performing a long computation might periodically check to see whether the
  38. user had pressed a cancel button. Throwing an exception could allow the
  39. operation to be cancelled gracefully. On the other hand, it would
  40. probably be inappropriate to throw and <i>handle</i> exceptions in the
  41. inner loop of this computation because that could have a significant
  42. performance impact.</p>
  43. <h3>How should I design my exception classes?</h3>
  44. <ol>
  45. <li><b>Inherit from <code>std::exception</code></b>. Except in *very*
  46. rare circumstances where you can't afford the cost of a virtual table,
  47. <code>std::exception</code> makes a reasonable exception base class,
  48. and when used universally, allows programmers to catch "everything"
  49. without resorting to <code>catch(...)</code>. For more about
  50. <code>catch(...)</code>, see below.</li>
  51. <li>
  52. <b><i>Don't</i> embed a std::string object</b> or any other data
  53. member or base class whose copy constructor could throw an exception.
  54. That could lead to termination during stack unwinding. Similarly,
  55. it's a bad idea to use a base or member whose ordinary constructor
  56. might throw, because, though not fatal, you will report a different
  57. exception than intended when that happens in a
  58. <i>throw-expression</i> such as:
  59. <blockquote>
  60. <pre>
  61. throw some_exception();
  62. </pre>
  63. </blockquote>
  64. <p>There are various ways to avoid copying string objects when
  65. exceptions are copied, including embedding a fixed-length buffer in
  66. the exception object, or managing strings via reference-counting.
  67. However, consider the next point before pursuing either of these
  68. approaches.</p>
  69. </li>
  70. <li><b>Format the <code>what()</code> message on demand</b>, if you
  71. feel you really must format the message. Formatting an exception error
  72. message is typically a memory-intensive operation that could
  73. potentially throw an exception. This is an operation best delayed until
  74. after stack unwinding has occurred, and presumably, released some
  75. resources. It's a good idea in this case to protect your
  76. <code>what()</code> function with a <code>catch(...)</code> block so
  77. that you have a fallback in case the formatting code throws</li>
  78. <li><b>Don't worry <i>too</i> much about the <code>what()</code>
  79. message</b>. It's nice to have a message that a programmer stands a
  80. chance of figuring out, but you're very unlikely to be able to compose
  81. a relevant and <i>user</i>-comprehensible error message at the point an
  82. exception is thrown. Certainly, internationalization is beyond the
  83. scope of the exception class author. <a href=
  84. "../people/peter_dimov.htm">Peter Dimov</a> makes an excellent argument
  85. that the proper use of a <code>what()</code> string is to serve as a
  86. key into a table of error messages. Now if only we could get
  87. standardized <code>what()</code> strings for exceptions thrown by the
  88. standard library...</li>
  89. <li><b>Make your exception class immune to double-destruction</b> if
  90. possible. Unfortunately, several popular compilers occasionally cause
  91. exception objects to be destroyed twice. If you can arrange for that to
  92. be harmless (e.g. by zeroing deleted pointers) your code will be more
  93. robust.</li>
  94. </ol>
  95. <h3>What About Programmer Errors?</h3>
  96. <p>As a developer, if I have violated a precondition of a library I'm
  97. using, I don't want stack unwinding. What I want is a core dump or the
  98. equivalent - a way to inspect the state of the program at the exact point
  99. where the problem was detected. That usually means <tt>assert()</tt> or
  100. something like it.</p>
  101. <p>Sometimes it is neccessary to have resilient APIs which can stand up
  102. to nearly any kind of client abuse, but there is usually a significant
  103. cost to this approach. For example, it usually requires that each object
  104. used by a client be tracked so that it can be checked for validity. If
  105. you need that sort of protection, it can usually be provided as a layer
  106. on top of a simpler API. Beware half-measures, though. An API which
  107. promises resilience against some, but not all abuse is an invitation to
  108. disaster. Clients will begin to rely on the protection and their
  109. expectations will grow to cover unprotected parts of the interface.</p>
  110. <p><b>Note for Windows developers</b>: unfortunately, the native
  111. exception-handling used by most Windows compilers actually throws an
  112. exception when you use <tt>assert()</tt>. Actually, this is true of other
  113. programmer errors such as segmentation faults and divide-by-zero errors.
  114. One problem with this is that if you use JIT (Just In Time) debugging,
  115. there will be collateral exception-unwinding before the debugger comes up
  116. because <code>catch(...)</code> will catch these not-really-C++
  117. exceptions. Fortunately, there is a simple but little-known workaround,
  118. which is to use the following incantation:</p>
  119. <blockquote>
  120. <pre>
  121. extern "C" void straight_to_debugger(unsigned int, EXCEPTION_POINTERS*)
  122. {
  123. throw;
  124. }
  125. extern "C" void (*old_translator)(unsigned, EXCEPTION_POINTERS*)
  126. = _set_se_translator(straight_to_debugger);
  127. </pre>
  128. </blockquote>
  129. This technique doesn't work if the SEH is raised from within a catch
  130. block (or a function called from within a catch block), but it still
  131. eliminates the vast majority of JIT-masking problems.
  132. <h3>How should I handle exceptions?</h3>
  133. <p>Often the best way to deal with exceptions is to not handle them at
  134. all. If you can let them pass through your code and allow destructors to
  135. handle cleanup, your code will be cleaner.</p>
  136. <h4>Avoid <code>catch(...)</code> when possible</h4>
  137. Unfortunately, operating systems other than Windows also wind non-C++
  138. "exceptions" (such as thread cancellation) into the C++ EH machinery, and
  139. there is sometimes no workaround corresponding to the
  140. <code>_set_se_translator</code> hack described above. The result is that
  141. <code>catch(...)</code> can have the effect of making some unexpected
  142. system notification at a point where recovery is impossible look just
  143. like a C++ exception thrown from a reasonable place, invalidating the
  144. usual safe assumptions that destructors and catch blocks have taken valid
  145. steps to ensure program invariants during unwinding. I reluctantly
  146. concede this point to Hillel Y. Sims, who beat it into me (&lt;wink&gt;):
  147. until all OSes are "fixed", if every exception were derived from
  148. <code>std::exception</code> and everyone substituted
  149. <code>catch(std::exception&amp;)</code> for <code>catch(...)</code>, the
  150. world would be a better place.
  151. <hr>
  152. <p>&copy; Copyright David Abrahams 2001. Permission to copy, use, modify,
  153. sell and distribute this document is granted provided this copyright
  154. notice appears in all copies. This document is provided "as is" without
  155. express or implied warranty, and with no claim as to its suitability for
  156. any purpose.</p>
  157. <p>Revised
  158. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->
  159. 22 March, 2003<!--webbot bot="Timestamp" endspan i-checksum="34359" -->
  160. </p>
  161. </body>
  162. </html>
粤ICP备19079148号