title: Is catching an exception by reference dangerous?
body: <p>Please take a look at the following exception throwing and catching:</p>
  <pre><code>void some_function() {
      // Was std::exception(&quot;message&quot;) in original post, which won't compile
      throw std::runtime_error(&quot;some error message&quot;); 
  }
  
  int main(int argc, char **argv) {
      try {
          some_function();
      } catch (const std::exception&amp; e) {
          std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
          exit(1);
      }
      return 0;
  }
  </code></pre>
  <p>Is it safe to catch the thrown exception by reference?</p>
  <p>My concern is because the exception <code>e</code> is actually <strong>placed on the stack</strong> of <code>some_function()</code>. But <code>some_function()</code> has just returned, causing <code>e</code> to be destructed. So actually now <code>e</code> points to a destructed object.</p>
  <p>Is my concern correct?</p>
  <p>What is the correct way to pass the exception without copying it by value? Should I throw <code>new std::exception()</code> so it is placed in the dynamic memory?</p>