frequently_asked_questions.html 9.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
  2. 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
  4. <head>
  5. <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
  6. <title>frequently asked questions</title>
  7. <link href='reno.css' type='text/css' rel='stylesheet'/>
  8. </head>
  9. <body>
  10. <div class="body-0">
  11. <div class="body-1">
  12. <div class="body-2">
  13. <div>
  14. <div id="boost_logo">
  15. <a href="http://www.boost.org"><img style="border:0" src="../../../boost.png" alt="Boost" width="277" height="86"/></a>
  16. </div>
  17. <h1>Boost Exception</h1>
  18. </div>
  19. <!-- Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. -->
  20. <!-- Distributed under the Boost Software License, Version 1.0. (See accompanying -->
  21. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  22. <div class="RenoIncludeDIV"><div class="RenoAutoDIV"><h2>Frequently Asked Questions</h2>
  23. </div>
  24. <h3>What is the cost of calling boost::throw_exception?</h3>
  25. <p>The cost is that boost::<span class="RenoLink"><a href="exception.html">exception</a></span> is added as a base of the exception emitted by boost::<span class="RenoLink"><a href="throw_exception.html">throw_exception</a></span> (unless the passed type already derives from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>.)</p>
  26. <p>Calling boost::<span class="RenoLink"><a href="throw_exception.html">throw_exception</a></span> does not cause dynamic memory allocations.</p>
  27. <h3>What is the cost of BOOST_THROW_EXCEPTION?</h3>
  28. <p>In addition to calling boost::<span class="RenoLink"><a href="throw_exception.html">throw_exception</a></span>, <span class="RenoLink"><a href="BOOST_THROW_EXCEPTION.html">BOOST_THROW_EXCEPTION</a></span> invokes __FILE__, __LINE__ and the <span class="RenoLink"><a href="configuration_macros.html">BOOST_THROW_EXCEPTION_CURRENT_FUNCTION</a></span> macros. The space required to store the information is already included in sizeof(boost::<span class="RenoLink"><a href="exception.html">exception</a></span>).</p>
  29. <p>Calling <span class="RenoLink"><a href="BOOST_THROW_EXCEPTION.html">BOOST_THROW_EXCEPTION</a></span> does not cause dynamic memory allocations.</p>
  30. <h3>Should I use boost::throw_exception or BOOST_THROW_EXCEPTION or just throw?</h3>
  31. <p>The benefit of calling boost::<span class="RenoLink"><a href="throw_exception.html">throw_exception</a></span> instead of using throw directly is that it ensures that the emitted exception derives from boost::<span class="RenoLink"><a href="exception.html">exception</a></span> and that it is compatible with boost::<span class="RenoLink"><a href="current_exception.html">current_exception</a></span>.</p>
  32. <p>The <span class="RenoLink"><a href="BOOST_THROW_EXCEPTION.html">BOOST_THROW_EXCEPTION</a></span> macro also results in a call to boost::<span class="RenoLink"><a href="throw_exception.html">throw_exception</a></span>, but in addition it records in the exception object the __FILE__ and __LINE__ of the throw, as well as the pretty name of the function that throws. This enables boost::<span class="RenoLink"><a href="diagnostic_information.html">diagnostic_information</a></span> to compose a more useful, if not user-friendly message.</p>
  33. <p>Typical use of boost::<span class="RenoLink"><a href="diagnostic_information.html">diagnostic_information</a></span> is:</p>
  34. <pre>catch(...)
  35. {
  36. std::cerr &lt;&lt;
  37. "Unexpected exception, diagnostic information follows:\n" &lt;&lt;
  38. <span class="RenoLink"><a href="current_exception_diagnostic_information.html">current_exception_diagnostic_information</a></span>();
  39. }</pre>
  40. <p>This is a possible message it may display -- the information in the first line is only available if <span class="RenoLink"><a href="BOOST_THROW_EXCEPTION.html">BOOST_THROW_EXCEPTION</a></span> was used to throw:</p>
  41. <pre>example_io.cpp(70): Throw in function class boost::shared_ptr&lt;struct _iobuf&gt; __cdecl my_fopen(const char *,const char *)
  42. Dynamic exception type: class boost::exception_detail::clone_impl&lt;class fopen_error&gt;
  43. std::exception::what: example_io error
  44. [struct boost::<span class="RenoLink"><a href="errinfo_api_function.html">errinfo_api_function</a></span>_ *] = fopen
  45. [struct boost::<span class="RenoLink"><a href="errinfo_errno.html">errinfo_errno</a></span>_ *] = 2, "No such file or directory"
  46. [struct boost::<span class="RenoLink"><a href="errinfo_file_name.html">errinfo_file_name</a></span>_ *] = tmp1.txt
  47. [struct boost::<span class="RenoLink"><a href="errinfo_file_open_mode.html">errinfo_file_open_mode</a></span>_ *] = rb</pre>
  48. <p>In some development environments, the first line in that message can be clicked to show the location of the throw in the debugger, so it's easy to set a break point and run again to see the unexpected throw in the context of its call stack.</p>
  49. <h3>Why doesn't boost::exception derive from std::exception?</h3>
  50. <p>Despite that <span class="RenoLink"><a href="using_virtual_inheritance_in_exception_types.html">virtual inheritance should be used in deriving from base exception types</a></span>, quite often exception types (including the ones defined in the standard library) don't derive from std::exception virtually.</p>
  51. <p>If boost::<span class="RenoLink"><a href="exception.html">exception</a></span> derives from std::exception, using the <span class="RenoLink"><a href="enable_error_info.html">enable_error_info</a></span> function with such user-defined types would introduce dangerous ambiguity which would break all catch(std::exception &amp;) statements.</p>
  52. <p>Of course, boost::<span class="RenoLink"><a href="exception.html">exception</a></span> should not be used to replace std::exception as a base type in exception type hierarchies. Instead, it should be included as a virtual base, in addition to std::exception (which should probably also be derived virtually.)</p>
  53. <h3>Why is boost::exception abstract?</h3>
  54. <p>To prevent exception-neutral contexts from erroneously erasing the type of the original exception when adding <span class="RenoLink"><a href="error_info.html">error_info</a></span> to an active exception object:</p>
  55. <pre>catch( boost::<span class="RenoLink"><a href="exception.html">exception</a></span> &amp; e )
  56. {
  57. e <span class="RenoLink"><a href="exception_operator_shl.html">&lt;&lt;</a></span> foo_info(foo);
  58. throw e; //Compile error: boost::<span class="RenoLink"><a href="exception.html">exception</a></span> is abstract
  59. }</pre>
  60. <p>The correct code is:</p>
  61. <pre>catch( boost::<span class="RenoLink"><a href="exception.html">exception</a></span> &amp; e )
  62. {
  63. e <span class="RenoLink"><a href="exception_operator_shl.html">&lt;&lt;</a></span> foo_info(foo);
  64. throw; //Okay, re-throwing the original exception object.
  65. }</pre>
  66. <h3>Why use operator&lt;&lt; overload for adding info to exceptions?</h3>
  67. <p>Before throwing an object of type that derives from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>, it is often desirable to add one or more <span class="RenoLink"><a href="error_info.html">error_info</a></span> objects in it. The syntactic sugar provided by <span class="RenoLink"><a href="exception_operator_shl.html">operator&lt;&lt;</a></span> allows this to be done directly in a throw expression:</p>
  68. <pre>throw error() <span class="RenoLink"><a href="exception_operator_shl.html">&lt;&lt;</a></span> foo_info(foo) <span class="RenoLink"><a href="exception_operator_shl.html">&lt;&lt;</a></span> bar_info(bar);</pre>
  69. <h3>Why is operator&lt;&lt; allowed to throw?</h3>
  70. <p>This question is referring to the following issue. Consider this throw statement example:</p>
  71. <pre>throw file_open_error() <span class="RenoLink"><a href="exception_operator_shl.html">&lt;&lt;</a></span> file_name(fn);</pre>
  72. <p>The intention here is to throw a file_open_error, however if <span class="RenoLink"><a href="exception_operator_shl.html">operator&lt;&lt;</a></span> fails to copy the std::string contained in the file_name <span class="RenoLink"><a href="error_info.html">error_info</a></span> wrapper, a std::bad_alloc could propagate instead. This behavior seems undesirable to some programmers.</p>
  73. <p>Bjarne Stroustrup, The C++ Programming Language, 3rd Edition, page 371:</p>
  74. <blockquote><p><i>"Throwing an exception requires an object to throw. A C++ implementation is required to have enough spare memory to be able to throw bad_alloc in case of memory exhaustion. However, it is possible that throwing some other exception will cause memory exhaustion."</i></p></blockquote>
  75. <p>Therefore, the language itself does not guarantee that an attempt to throw an exception is guaranteed to throw an object of the specified type; propagating a std::bad_alloc seems to be a possibility even outside of the scope of Boost Exception.</p>
  76. </div><div class="RenoAutoDIV"><div class="RenoHR"><hr/></div>
  77. See also: <span class="RenoPageList"><a href="boost-exception.html">Boost Exception</a>&nbsp;| <a href="motivation.html">Motivation</a></span>
  78. </div>
  79. <!-- Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. -->
  80. <!-- Distributed under the Boost Software License, Version 1.0. (See accompanying -->
  81. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  82. <div id="footer">
  83. <p>
  84. <a class="logo" href="http://jigsaw.w3.org/css-validator/check/referer"><img class="logo_pic" src="valid-css.png" alt="Valid CSS" height="31" width="88"/></a>
  85. <a class="logo" href="http://validator.w3.org/check?uri=referer"><img class="logo_pic" src="valid-xhtml.png" alt="Valid XHTML 1.0" height="31" width="88"/></a>
  86. <small>Copyright (c) 2006-2009 by Emil Dotchevski and Reverge Studios, Inc.<br/>
  87. Distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License, Version 1.0</a>.</small>
  88. </p>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </body>
  94. </html>