title: Exception class copy constructor
body: <p>I'm testing how throwing when doing stack unwinding calls <code>std::terminate</code> by having the copy constructor of the class that is used to throw, throwing. </p>
  
  <p>Per C++14 N4296 - §15.1.3:</p>
  
  <blockquote>
    <p>Throwing an exception copy-initializes (8.5, 12.8) a temporary object,
    called the exception object. The temporary is an lvalue and is used to
    initialize the variable declared in the matching handler (15.3).</p>
  </blockquote>
  
  <pre><code>class X
  {
    public:
  
      X() = default;
  
      X(const X&amp;)
      {
        throw std::runtime_error{"X copy constructor"};
      }
  };
  
  int main()
  {
    try
    {
      throw X{};
    }
    catch(const std::exception&amp; e)
    {
      std::cout &lt;&lt; e.what() &lt;&lt; std::endl; // this line gets executed, does not terminate
    }
    catch(const X&amp; other)
    {
      std::cout &lt;&lt; "X exception" &lt;&lt; std::endl;
    }
  }
  </code></pre>
  
  <p>How come the above code doesn't call <code>std::terminate</code>? It is my understanding of the above code that the <code>throw X{}</code> inside the <code>try</code>, starts the stack winding and then the copy constructor gets called to call the handler, but the copy constructor throws as well. As a note if change the copy constructor's throw expression for <code>throw X{}</code> then it terminates. </p>