By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

Construction and destruction of objects when exceptions are thrown

P: n/a
I compiled this code on gcc 4.1.2 and the output I got seems to be a
bit weird.

Here is the code:

#include <iostream>

class A{
public:
A(){std::cout << "Constructor" << std::endl;}
~A(){std::cout << "Destructor" << std::endl;}
};

void display(){
A mystr;
throw mystr;
}

int main() {
try{
display();
}
catch(A str){
}
return 0;
}

The output happens to be:
Constructor
Destructor
Destructor
Destructor

I don't know if I am missing something. I checked all the references
that I have for exceptions. Looks like local objects get deleted
normally when exceptions are thrown. I cant seem to explain this
output. Would be grateful if the members here can help me with this.

Regards
Dana

Feb 20 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
th*************@gmail.com wrote:
I compiled this code on gcc 4.1.2 and the output I got seems to be a
bit weird.

Here is the code:

#include <iostream>

class A{
public:
A(){std::cout << "Constructor" << std::endl;}
~A(){std::cout << "Destructor" << std::endl;}
};

void display(){
A mystr;
throw mystr;
}

int main() {
try{
display();
}
catch(A str){
}
return 0;
}

The output happens to be:
Constructor
Destructor
Destructor
Destructor

I don't know if I am missing something. I checked all the references
that I have for exceptions. Looks like local objects get deleted
normally when exceptions are thrown. I cant seem to explain this
output. Would be grateful if the members here can help me with this.
Your 'A' class has a copy constructor which you forgot to consider.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 20 '07 #2

P: n/a
>>
>>I don't know if I am missing something. I checked all the references
that I have for exceptions. Looks like local objects get deleted
normally when exceptions are thrown. I cant seem to explain this
output. Would be grateful if the members here can help me with this.


Your 'A' class has a copy constructor which you forgot to consider.

V
It's just incredible how many times we get this post.

john
Feb 20 '07 #3

P: n/a
th*************@gmail.com wrote:
>
int main() {
try{
display();
}
catch(A str){
catch(A &str){ <-- catch using refs
On a related note, please make it a habit to catch by reference. It's
highly recommended (not required).

http://www.parashift.com/c++-faq-lit....html#faq-17.7

}
return 0;
}
Feb 20 '07 #4

P: n/a
John Harrison wrote:
>>I don't know if I am missing something. I checked all the references
that I have for exceptions. Looks like local objects get deleted
normally when exceptions are thrown. I cant seem to explain this
output. Would be grateful if the members here can help me with this.


Your 'A' class has a copy constructor which you forgot to consider.

V

It's just incredible how many times we get this post.
If it's so frequently asked (which I can't vouch for), then perhaps
somebody <wink-winkcould write up an entry for Marshall Cline to
add to the FAQ Lite...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.