title: C++ Exception Throw/Catch Optimizations
body: <p>It seems to me that if you have some C++ code like this:</p>
  
  <pre><code>int f()
  {
    try {
      if( do_it() != success ) {
        throw do_it_failure();
      }
    } catch( const std::exception &amp;e ) {
      show_error( e.what() );
    }
  }
  </code></pre>
  
  <p>The C++ compiler should be able to optimize the throw and catch into almost a simple goto.</p>
  
  <p>However, it seems to me from my experience viewing disassembly and stepping through code that the compilers <em>always</em> jump through the very messy exception handling libraries.</p>
  
  <p>Why do they do that? Is there some language requirement that prevents optimizing? What if it was:</p>
  
  <pre><code>int f()
  {
    try { throw std::runtime_error("Boo!"); }
    catch ( const std::exception &amp;e ) { std::cout &lt;&lt; e.what() &lt;&lt; std::endl; }
  }
  </code></pre>
  
  <p>Why does the compiler not just rewrite that as</p>
  
  <pre><code>int f()
  {
    std::cout &lt;&lt; "Boo!" &lt;&lt; std::endl;
  }
  </code></pre>