Boost C++ Libraries: Ticket #5260: scope_guard safe_execute could support try/catch when BOOST_NO_EXCPTIONS not defined https://svn.boost.org/trac10/ticket/5260 <p> Hi, </p> <p> Since I use boost library for many things, I thought I would take advantage of the scope guard implementation in boost/multi_index/detail/scope_guard.hpp. I know that is not standard practice, but thought it would be better then implementing my own ;-) </p> <p> However I discovered the hard way (got an unhandled exception) that safe_execute does not support try/catch. From the comments </p> <ul><li> - safe_execute does not feature a try-catch protection, so we can </li><li> use this even if BOOST_NO_EXCEPTIONS is defined. </li></ul><p> I verified that this is still the case in the latest release version. (I use an older version). </p> <p> It seems it could support try/catch while still honoring BOOST_NO_EXCEPTIONS when defined by wrapping the try and catch portions in #ifdefs as follows </p> <blockquote> <p> template&lt;typename J&gt; static void safe_execute(J&amp; j) { </p> <blockquote> <p> if(!j.dismissed_) </p> </blockquote> <p> #ifndef BOOST_NO_EXCEPTIONS </p> <blockquote> <blockquote> <p> try { </p> </blockquote> </blockquote> <p> #endif </p> <blockquote> <blockquote> <blockquote> <p> j.execute(); </p> </blockquote> </blockquote> </blockquote> <p> #ifndef BOOST_NO_EXCEPTIONS </p> <blockquote> <blockquote> <p> } catch (...) { } </p> </blockquote> </blockquote> <p> #endif } </p> </blockquote> <p> By doing that when using exceptions it will work as the original code this is based on works. And when BOOST_NO_EXCEPTIONS is defined it will work as it does now. </p> <p> The following example code will generate an unhandled exception with out the above mods. The same example with the above mods will also generate the unhandled exception if BOOST_NO_EXCEPTIONS is defined (you can uncomment the #define BOOST_NO_EXCEPTIONS to test that). It will not generate an unhandled exception when BOOST_NO_EXCEPTIONS is not defined. </p> <p> <em> #define BOOST_NO_EXCEPTIONS #include &lt;iostream&gt; #include &lt;boost/multi_index/detail/scope_guard.hpp&gt; </em></p> <p> void bar() { </p> <blockquote> <p> throw std::exception("testing"); </p> </blockquote> <p> } </p> <p> void foo() { </p> <blockquote> <p> boost::multi_index::detail::scope_guard barGuard = boost::multi_index::detail::make_guard(&amp;bar); </p> </blockquote> <p> } </p> <p> int main(int argc, char* argv[]) { </p> <blockquote> <p> foo(); return 0; </p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5260 Trac 1.4.3 Joaquín M López Muñoz Fri, 04 Mar 2011 08:19:06 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/5260#comment:1 https://svn.boost.org/trac10/ticket/5260#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Really, the backup code provided to a scope guard should not destroy, but the proposed addition is simple enough and admittedly was in the original code by Alexandrescu. Change commited at <a class="changeset" href="https://svn.boost.org/trac10/changeset/69540" title="closed #5260">[69540]</a>. </p> Ticket