tutorial_enable_error_info.html 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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>integrating boost exception in existing exception class hierarchies</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>Integrating Boost Exception in Existing Exception Class Hierarchies</h2>
  23. </div>
  24. <p>Some exception hierarchies can not be modified to make boost::<span class="RenoLink"><a href="exception.html">exception</a></span> a base type. In this case, the <span class="RenoLink"><a href="enable_error_info.html">enable_error_info</a></span> function template can be used to make exception objects derive from boost::<span class="RenoLink"><a href="exception.html">exception</a></span> anyway. Here is an example:</p>
  25. <pre>#include &lt;<span class="RenoLink"><a href="boost_exception_all_hpp.html">boost/exception/all.hpp</a></span>&gt;
  26. #include &lt;stdexcept&gt;
  27. typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span>&lt;struct tag_std_range_min,size_t&gt; std_range_min;
  28. typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span>&lt;struct tag_std_range_max,size_t&gt; std_range_max;
  29. typedef boost::<span class="RenoLink"><a href="error_info.html">error_info</a></span>&lt;struct tag_std_range_index,size_t&gt; std_range_index;
  30. template &lt;class T&gt;
  31. class
  32. my_container
  33. {
  34. public:
  35. size_t size() const;
  36. T const &amp;
  37. operator[]( size_t i ) const
  38. {
  39. if( i &gt; size() )
  40. throw boost::<span class="RenoLink"><a href="enable_error_info.html">enable_error_info</a></span>(std::range_error("Index out of range")) &lt;&lt;
  41. std_range_min(0) &lt;&lt;
  42. std_range_max(size()) &lt;&lt;
  43. std_range_index(i);
  44. //....
  45. }
  46. };
  47. </pre>
  48. <p>The call to <span class="RenoLink"><a href="enable_error_info.html">enable_error_info</a></span>&lt;T&gt; gets us an object of <i>unspecified type</i> which is guaranteed to derive from both boost::<span class="RenoLink"><a href="exception.html">exception</a></span> and T. This makes it possible to use <span class="RenoLink"><a href="exception_operator_shl.html">operator&lt;&lt;</a></span> to store additional information in the exception object. The exception can be intercepted as T &amp;, so existing exception handling will not break. It can also be intercepted as boost::<span class="RenoLink"><a href="exception.html">exception</a></span> &amp;, so that <span class="RenoLink"><a href="tutorial_transporting_data.html">more information can be added to the exception at a later time</a></span>.</p>
  49. </div><div class="RenoAutoDIV"><div class="RenoHR"><hr/></div>
  50. See also: <span class="RenoPageList"><a href="boost-exception.html">Boost Exception</a>&nbsp;| <a href="motivation.html">Motivation</a></span>
  51. </div>
  52. <!-- Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. -->
  53. <!-- Distributed under the Boost Software License, Version 1.0. (See accompanying -->
  54. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  55. <div id="footer">
  56. <p>
  57. <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>
  58. <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>
  59. <small>Copyright (c) 2006-2009 by Emil Dotchevski and Reverge Studios, Inc.<br/>
  60. Distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License, Version 1.0</a>.</small>
  61. </p>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </body>
  67. </html>