error_handling.html 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
  2. <meta name="generator" content="HTML Tidy, see www.w3.org">
  3. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  4. <title>Error and Exception Handling</title>
  5. <h1>Error and Exception Handling</h1>
  6. <h2>References</h2>
  7. <p>The following paper is a good introduction to some of the issues of
  8. writing robust generic components:
  9. <blockquote>
  10. <a href="generic_exception_safety.html">D. Abrahams: ``Exception Safety
  11. in Generic Components''</a>, originally published in <a href=
  12. "http://www.springer.de/cgi-bin/search_book.pl?isbn=3-540-41090-2">M.
  13. Jazayeri, R. Loos, D. Musser (eds.): Generic Programming, Proc. of a
  14. Dagstuhl Seminar, Lecture Notes on Computer Science 1766</a>
  15. </blockquote>
  16. <h2>Guidelines</h2>
  17. <h3>When should I use exceptions?</h3>
  18. <p>The simple answer is: ``whenever the semantic and performance
  19. characteristics of exceptions are appropriate.''
  20. <p>An oft-cited guideline is to ask yourself the question ``is this an
  21. exceptional (or unexpected) situation?'' This guideline has an attractive
  22. ring to it, but is usually a mistake. The problem is that one person's
  23. ``exceptional'' is another's ``expected'': when you really look at the
  24. terms carefully, the distinction evaporates and you're left with no
  25. guideline. After all, if you check for an error condition, then in some
  26. sense you expect it to happen, or the check is wasted code.
  27. <p>A more appropriate question to ask is: ``do we want stack unwinding
  28. here?'' Because actually handling an exception is likely to be
  29. significantly slower than executing mainline code, you should also ask:
  30. ``Can I afford stack unwinding here?'' For example, a desktop application
  31. performing a long computation might periodically check to see whether the
  32. user had pressed a cancel button. Throwing an exception could allow the
  33. operation to be cancelled gracefully. On the other hand, it would probably
  34. be inappropriate to throw and <i>handle</i> exceptions in the inner loop of
  35. this computation because that would have a significant performance impact.
  36. <h3>What About Programmer Errors?</h3>
  37. <p>As a developer, if I have violated a precondition of a library I'm
  38. using, I don't want stack unwinding. What I want is a core dump or the
  39. equivalent - a way to inspect the state of the program at the exact point
  40. where the problem was detected. That usually means <tt>assert()</tt> or
  41. something like it.
  42. <p>Sometimes it is neccessary to have resilient APIs which can stand up to
  43. nearly any kind of client abuse, but there is usually a significant cost to
  44. this approach. For example, it usually requires that each object used by a
  45. client be tracked so that it can be checked for validity. If you need that
  46. sort of protection, it can usually be provided as a layer on top of a
  47. simpler API. Beware half-measures, though. An API which promises resilience
  48. against some, but not all abuse is an invitation to disaster. Clients will
  49. begin to rely on the protection and their expectations will grow to cover
  50. unprotected parts of the interface.
  51. <p><b>Note for Windows developers</b>: unfortunately, the native
  52. exception-handling used by most Windows compilers actually throws an
  53. exception when you use <tt>assert()</tt>. Actually, this is true of other
  54. programmer errors such as segmentation faults and divide-by-zero errors.
  55. One problem with this is that if you use JIT (Just In Time) debugging,
  56. there will be collateral exception-unwinding before the debugger comes up.
  57. Fortunately, there is a simple but little-known workaround, which is to use
  58. the following incantation:
  59. <blockquote>
  60. <pre>
  61. extern "C" void straight_to_debugger(unsigned int, EXCEPTION_POINTERS*)
  62. {
  63. throw;
  64. }
  65. extern "C" void (*old_translator)(unsigned, EXCEPTION_POINTERS*)
  66. = _set_se_translator(straight_to_debugger);
  67. </pre>
  68. </blockquote>
  69. <hr>
  70. <p>&copy; Copyright David Abrahams 2001. Permission to copy, use, modify,
  71. sell and distribute this document is granted provided this copyright notice
  72. appears in all copies. This document is provided "as is" without express or
  73. implied warranty, and with no claim as to its suitability for any purpose.
  74. <p>Revised
  75. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->17 March, 2001<!--webbot bot="Timestamp" endspan i-checksum="40399" -->
粤ICP备19079148号