473,378 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Construction and destruction of objects when exceptions are thrown

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
4 1268
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
>>
>>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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pierre-Eric.Melchy | last post by:
Hello, I have a class measurement representing a physical measurement. Different objects in this class represent laboratory equipment, which might raise an exception (e.g. overtemperature). ...
15
by: Ant | last post by:
Hi, This might seem like a strange question but I'm wondering how other developers go about choosing the appropriate Exception objects to use in their catch statements. Currently, I choose them...
8
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an...
9
by: plahey | last post by:
I have been dabbling in Python for a while now. One of the things that really appeals to me is that I can seem to be able to use C++-style RAII idioms to deal with resource management issues. ...
4
by: ma740988 | last post by:
Referencing source snippet below, the actual contruction of the foo objects is done in a class. In that regard, I chose methods, class1_construct and class2_construct for demonstration purposes....
7
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case...
2
by: barcaroller | last post by:
At which point is an object considered to be completely constructed? X::X() { // constructor stuff throw "Exception"; // premature exit } X* x;
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.