Possible Duplicate:
Scope of exception object in C++
I have the following catch clauses:
catch(Widget w);
catch(Widget& w);
void passAndThrowWidget() {
Widget localWidget;
throw localWidget;
}
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.
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.
Thanks!