473,398 Members | 2,525 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,398 software developers and data experts.

Basic question C++ exception

Hi All,

I am not able to figure out what exactly happening in below code. what
is control flow. Can anyone clear my confusion?

Code:
class A
{
public:
A(){cout<<"In Constructor\n";}
//// A(const A&){cout<<"In Copy Constructor\n";} // if we uncomment
this, we see different //output .
~A(){cout<<"In Destructor\n";}
};
try{
cout<<"In Try\n";
throw A();
}
catch(A &a)
{
cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Destructor
In Catch
In Destructor

Question 1. I don't know why two times destructor has been called. I
understand, since i am using reference, so there would not be any new
object. then why two times destructor got called.

Question 2.

class A
{
public:
A(){cout<<"In Constructor\n";}
A(const A&){cout<<"In Copy Constructor\n";}
~A(){cout<<"In Destructor\n";}
};
try{
cout<<"In Try\n";
throw A();
}
catch(A a)
{
cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Copy Constructor
In Catch
In Destructor
In Destructor

Why object created by throw A() has not been deleted while exiting try
block in above code?

thanks in advance.

Regards,
Jun 29 '08 #1
5 1517
On 2008-06-29 17:34, Vijay wrote:
Hi All,

I am not able to figure out what exactly happening in below code. what
is control flow. Can anyone clear my confusion?

Code:
class A
{
public:
A(){cout<<"In Constructor\n";}
//// A(const A&){cout<<"In Copy Constructor\n";} // if we uncomment
this, we see different //output .
~A(){cout<<"In Destructor\n";}
};
try{
cout<<"In Try\n";
throw A();
}
catch(A &a)
{
cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Destructor
In Catch
In Destructor

Question 1. I don't know why two times destructor has been called. I
understand, since i am using reference, so there would not be any new
object. then why two times destructor got called.
The throw-expression creates a temporary object (the exception object)
using the copy-constructor and that is the one destroyed after the
handler has run. The compiler is however allowed to optimise away the
extra object, what is funny is that my compiler chooses to do so only
when I uncomment the copy-constructor.
Question 2.

class A
{
public:
A(){cout<<"In Constructor\n";}
A(const A&){cout<<"In Copy Constructor\n";}
~A(){cout<<"In Destructor\n";}
};
try{
cout<<"In Try\n";
throw A();
}
catch(A a)
{
cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Copy Constructor
In Catch
In Destructor
In Destructor

Why object created by throw A() has not been deleted while exiting try
block in above code?
The object created by thow can not be destroyes before it has been used
to initialise the object in the exception-handler (the extra copy has
been eliminated). It will also live until the last exception handler has
run (if the object is destroyed and you re-throw what would happen
then?), so it is the last destructor to run.

If I comment out the copy-constructor my compiler does not optimise away
the extra copy and I get the following result:

In Try
In Constructor // throw A(), followed by a copy-constructor
In Destructor // The object created by A() is destroyed
In Catch // a in the catch(A a) is copy-constructed first
In Destructor // The object created in the handler is destroyes
In Destructor // The exception object dies

--
Erik Wikström
Jun 29 '08 #2
On Jun 29, 12:41*pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-06-29 17:34, Vijay wrote:
Hi All,
I am not able to figure out what exactly happening in below code. what
is control flow. Can anyone clear my confusion?
Code:
class A
{
public:
* *A(){cout<<"In Constructor\n";}
//// * * * A(const A&){cout<<"In Copy Constructor\n";} // if we uncomment
this, we see different //output .
* * ~A(){cout<<"In Destructor\n";}
};
try{
* *cout<<"In Try\n";
* *throw A();
* *}
catch(A &a)
{
* *cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Destructor
In Catch
In Destructor
Question 1. I don't know why two times destructor has been called. I
understand, since i am using reference, so there would not be any new
object. then why two times destructor got called.

The throw-expression creates a temporary object (the exception object)
using the copy-constructor and that is the one destroyed after the
handler has run. The compiler is however allowed to optimise away the
extra object, what is funny is that my compiler chooses to do so only
when I uncomment the copy-constructor.
yes. even i was thinking same. but was confused why compiler shows
different behavior after uncommenting the copy constructor. Thanks for
you reply

>
Question 2.
class A
{
public:
* *A(){cout<<"In Constructor\n";}
* * * *A(const A&){cout<<"In Copy Constructor\n";}
* * ~A(){cout<<"In Destructor\n";}
};
try{
* *cout<<"In Try\n";
* *throw A();
* *}
catch(A a)
{
* *cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Copy Constructor
In Catch
In Destructor
In Destructor
Why object created by throw A() has not been deleted while exiting try
block in above code?

The object created by thow can not be destroyes before it has been used
to initialise the object in the exception-handler (the extra copy has
been eliminated). It will also live until the last exception handler has
run (if the object is destroyed and you re-throw what would happen
then?), so it is the last destructor to run.

If I comment out the copy-constructor my compiler does not optimise away
the extra copy and I get the following result:

In Try
In Constructor * * // throw A(), followed by a copy-constructor
In Destructor * * *// The object created by A() is destroyed
In Catch * * * * * // a in the catch(A a) is copy-constructed first
In Destructor * * *// The object created in the handler is destroyes
In Destructor * * *// The exception object dies
Thanks. I think, because of compiler optimization, i am getting
confused.
one more point: in your above result, i didnt get two lines
In Destructor // The object created by A() is destroyed
and
In Destructor // The exception object dies

object created by A() is exception object. right?
If no, then when exception object is created which dies in last line?

Thanks a lot :)
>
--
Erik Wikström
Jun 29 '08 #3
On 2008-06-29 20:28, Vijay wrote:
On Jun 29, 12:41 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2008-06-29 17:34, Vijay wrote:
Question 2.
class A
{
public:
A(){cout<<"In Constructor\n";}
A(const A&){cout<<"In Copy Constructor\n";}
~A(){cout<<"In Destructor\n";}
};
try{
cout<<"In Try\n";
throw A();
}
catch(A a)
{
cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Copy Constructor
In Catch
In Destructor
In Destructor
Why object created by throw A() has not been deleted while exiting try
block in above code?

The object created by thow can not be destroyes before it has been used
to initialise the object in the exception-handler (the extra copy has
been eliminated). It will also live until the last exception handler has
run (if the object is destroyed and you re-throw what would happen
then?), so it is the last destructor to run.

If I comment out the copy-constructor my compiler does not optimise away
the extra copy and I get the following result:

In Try
In Constructor // throw A(), followed by a copy-constructor
In Destructor // The object created by A() is destroyed
In Catch // a in the catch(A a) is copy-constructed first
In Destructor // The object created in the handler is destroyes
In Destructor // The exception object dies

Thanks. I think, because of compiler optimization, i am getting
confused.
one more point: in your above result, i didnt get two lines
In Destructor // The object created by A() is destroyed
and
In Destructor // The exception object dies

object created by A() is exception object. right?
If no, then when exception object is created which dies in last line?
No, the exception object is a copy of the object created by A(), unless
the compiler uses the optimisation, in which case the exception-object
and the object created by A() is the same (and in that case it will no
be destroyed until the end of the exception-handler).

--
Erik Wikström
Jun 29 '08 #4
On Jun 29, 2:55*pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-06-29 20:28, Vijay wrote:
On Jun 29, 12:41 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-06-29 17:34, Vijay wrote:
Question 2.
class A
{
public:
* *A(){cout<<"In Constructor\n";}
* * * *A(const A&){cout<<"In Copy Constructor\n";}
* * ~A(){cout<<"In Destructor\n";}
};
try{
* *cout<<"In Try\n";
* *throw A();
* *}
catch(A a)
{
* *cout<<"In Catch\n";
}
output:
In Try;
In Constructor
In Copy Constructor
In Catch
In Destructor
In Destructor
Why object created by throw A() has not been deleted while exiting try
block in above code?
The object created by thow can not be destroyes before it has been used
to initialise the object in the exception-handler (the extra copy has
been eliminated). It will also live until the last exception handler has
run (if the object is destroyed and you re-throw what would happen
then?), so it is the last destructor to run.
If I comment out the copy-constructor my compiler does not optimise away
the extra copy and I get the following result:
In Try
In Constructor * * // throw A(), followed by a copy-constructor
In Destructor * * *// The object created by A() is destroyed
In Catch * * * * * // a in the catch(A a) is copy-constructed first
In Destructor * * *// The object created in the handler is destroyes
In Destructor * * *// The exception object dies
Thanks. I think, because of compiler optimization, i am getting
confused.
one more point: in your above result, i didnt get two lines
In Destructor * * *// The object created by A() is destroyed
and
In Destructor * * *// The exception object dies
object created by A() is exception object. right?
If no, then when exception object is created which dies in last line?

No, the exception object is a copy of the object created by A(), unless
the compiler uses the optimisation, in which case the exception-object
and the object created by A() is the same (and in that case it will no
be destroyed until the end of the exception-handler).
Got it. Thanks :)
--
Erik Wikström
Jun 29 '08 #5
On Jun 29, 8:28 pm, Vijay <mt.vi...@gmail.comwrote:
On Jun 29, 12:41 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
[...]
Thanks. I think, because of compiler optimization, i am getting
confused.
one more point: in your above result, i didnt get two lines
In Destructor // The object created by A() is destroyed
and
In Destructor // The exception object dies

object created by A() is exception object. right?
If no, then when exception object is created which dies in last line?
Just a note, but in such experimenting, I would recommend
outputting the this pointer in your trace output. That way, you
can easily see which destructors are for which objects.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 30 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
4
by: chirag | last post by:
i wrote the following code for the comments given. however, i am getting some errors in it. it says local function definitation are illegal.. plese scan through the following code. thanks. void...
11
by: Lloyd Dupont | last post by:
(not I use 2.0, so new return a "normal" pointer and gcnew return a managed one, my question below regarding new concern plain standart C++ allocator) - if I use the default new operator, are all...
6
by: Erez Shor | last post by:
Hi, I need to build and asp page which access a remote windows server's registry and create a registry key. In order for the ASP page to be able to access the registry on the remote server I need...
8
by: Brian | last post by:
I have a vb.net application which runs fine when installed locally on a user machine but when the application is run from a network drive a security exception error occurs. I've set the intranet...
0
by: BobJones | last post by:
After inserting the third CD during the installation process, the program starts rolling back the installation and displays an error message. I am running Windows 2000 SP4, installing VB.Net from...
2
by: frossberg | last post by:
Hello! I tried to install the Visual Basic.NET Resource Kit (http://msdn.microsoft.com/vbasic/vbrkit/) but obviously something went very wrong and now it sems impossible both to repair and to...
3
by: masood.iqbal | last post by:
In all the sample code snippets of try-catch code blocks that I have seen, the catch block does one of the following three things: 1). exits the program (after spitting out a cerr message) 2). ...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.