473,786 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1550
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 objektorientier ter Datenverarbeitu ng
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
9290
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. # No warranty express or implied for the accuracy, fitness to purpose
4
4161
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 Stack::print() // Prints the contents of a stack from top to bottom. The stack // is not changed. Does not call any Stack member functions. { int item; if (aList.isEmpty())
11
1964
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 the instance variable initialize to NULL / 0 ? - if there is not enough memory what happend with new ? does it return NULL or throw an exception? - if new throw a native C++ exception what happen in Managed C++ ?! - if there is an exception in...
6
2408
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 it to run using credentials supplied by the user. When using basic authentication this is not an issue since the user has to provide a user name and password. But I don't want to use basic authentication so I created a login form and I am using...
8
3348
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 zone through the .Net Configuration wizard to Full Trust and the app still has a security exception. I've turned on Just in Time Debugging and noticed the security exception is happening at that below line of code. Dim rs As New...
0
1059
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 the Deluxe Learning Edition (Standard Edition). I have done the installation three times with virus protection and firewall turned OFF to be sure that I didn't make a simple mistake in the installation process, all with the same result. The error...
2
4883
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 remove (to start over from scratch) the installation. The messages popping up after choosing one of these two alternatives Repair/Remove Visual Basic.NET Resource Kit in the Visual Basic.NET Resource Kit Setup Wizard are in each case respectively: ...
3
2178
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). propagates the exception 3). throws yet another exception I have a need to do something different. I want to merely spit out a cerr message when I catch an exception, and then proceed with my business logic. I am thinking of something like...
3
1943
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 objects that I can iterate through relatively easily. I've included code at the bottom of this message. I can pretty easily iterate through my employee objects like so: Dim theEmployees As Employees = New Employees
0
9496
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.