C++ exceptions catch clause
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/1654150/scope-of-exception-object-in-c">Scope of exception object in C++</a>  </p>
</blockquote>



<p>I have the following catch clauses:</p>

<pre><code>catch(Widget w);
catch(Widget&amp; w);

void passAndThrowWidget() {
          Widget localWidget;
      throw localWidget;
}
</code></pre>

<p>If we catch Widget object by value, compiler will make copy so when we throw exception, localWidget goes out of scope, and we don't see any issues.</p>

<p>If we catch widget object byreference, according to reference concept, "w" points to same local Widget instead of copy. But i have seen most of the exceptions are caught by references in C++. My question how this works as "localWidget" goes out of scope when exception is thrown and catch by referense points to object which is destroyed.</p>

<p>Thanks!</p>
