Original Post
Hi. I am now studying exceptions in C++. I wrote code to test destruction of thrown exception objects and when they occur. The output is somewhat strange. Here is the code: The output is : Inside f Constructor X::XExc Destuctor X::XExc Exception handler Destuctor X::XExc So the first destructor destroys the original XExc and the second destroys the copy made when throw is encountered.(at least that's how i see it) But, notice I commented the copy-constructor. If I explicitly specify one, there's no Destructor before the handler, but there's no copy constructor output either. I am a little confused here. [Edited by - Mocanu Razvan on April 14, 2006 4:29:41 AM]
class X {
public:
class XExc {
public:
XExc() { cout << "Constructor X::XExc" << endl; }
// XExc(const XExc&) { cout << "Copy X::XExc" << endl; }
~XExc() { cout << "Destuctor X::XExc" << endl; }
};
void f() {
cout << "Inside f" << endl;
throw XExc(); }
};
int main() {
X x;
try {
x.f();
} catch(X::XExc&) {
cout << "Exception handler" << endl;
}
}