Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Construction and destruction of objects when exceptions are thrown

Reply
Thread Tools

Construction and destruction of objects when exceptions are thrown

 
 
thamizh.veriyan@gmail.com
Guest
Posts: n/a
 
      02-20-2007
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-20-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-20-2007
>>
>>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
 
Reply With Quote
 
Piyo
Guest
Posts: n/a
 
      02-20-2007
(E-Mail Removed) 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;
> }

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-20-2007
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-wink> could 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
who invented deterministic construction+destruction? Keith H Duggar C++ 5 06-18-2009 10:10 AM
Proper Destruction of Class Members when an Exception is Thrown inDestructor AnonMail2005@gmail.com C++ 8 03-02-2009 01:07 AM
virtual base class and construction/destruction order BeautifulMind C++ 7 02-08-2007 12:35 PM
Unhandled Exceptions thrown Winista ASP .Net 0 03-22-2006 08:02 PM
Default construction versus construction with initial values Ook C++ 10 10-08-2005 09:00 PM



Advertisments