473,796 Members | 2,652 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception in a derived class constructor

Some amount of memory is allocated inside the Base Constructor using
new. During the construction of a derived object an exception occurred
in the constructor of the derived class.

Will the memory get de allocated which got allocated in Base class
constructor? Will the Base class destructor get called?

class Base
{
int *p;

public:
B() { p = new int[100]; }

~B() { delete[] p; }
};

class Derived : public Base
{
public:
Derived ()
{
/* Exception Occurred!!!!!!! !!!!!! */
}
};

May 15 '06 #1
8 2228

Kannan skrev:
Some amount of memory is allocated inside the Base Constructor using
new. During the construction of a derived object an exception occurred
in the constructor of the derived class.

Will the memory get de allocated which got allocated in Base class
constructor? Will the Base class destructor get called?

[snip]

This really looks like something for the FAQ:

http://www.parashift.com/c++-faq-lite/

/Peter

May 15 '06 #2
dj
Kannan wrote:
Some amount of memory is allocated inside the Base Constructor using
new. During the construction of a derived object an exception occurred
in the constructor of the derived class.

Will the memory get de allocated which got allocated in Base class
constructor? Will the Base class destructor get called?

class Base
{
int *p;

public:
B() { p = new int[100]; }

~B() { delete[] p; }
};

class Derived : public Base
{
public:
Derived ()
{
/* Exception Occurred!!!!!!! !!!!!! */
}
};


I believe the c++ primer book specifically mentions that in such cases
the destructors are guaranteed to be called. Anyway, a debug step
through should answer your question without doubt.
May 15 '06 #3

dj skrev:
Kannan wrote:
Some amount of memory is allocated inside the Base Constructor using
new. During the construction of a derived object an exception occurred
in the constructor of the derived class.

Will the memory get de allocated which got allocated in Base class
constructor? Will the Base class destructor get called?
[snip]
I believe the c++ primer book specifically mentions that in such cases
the destructors are guaranteed to be called. Anyway, a debug step
through should answer your question without doubt.

Stepping through the debugger at best shows the behaviour of that
particular compiler - in debug mode. Why not simply look it up?
Probably it is even faster than checking your compilers implementation.
Certainly the look-up gives you far more value for the investment.

/Peter

May 15 '06 #4
> Some amount of memory is allocated inside the Base Constructor using
new. During the construction of a derived object an exception occurred
in the constructor of the derived class.

Will the memory get de allocated which got allocated in Base class
constructor? Will the Base class destructor get called?

class Base
{
int *p;

public:
B() { p = new int[100]; }

~B() { delete[] p; }

};

class Derived : public Base
{
public:
Derived ()
{
/* Exception Occurred!!!!!!! !!!!!! */
}


The short answer is: Yes. The base destructor is guaranteed to be
called. And any objects in the base class and the derived class already
constructed (i.e. aggregate objects). But the tricky part is that the
destructor of the derived object itself is *not* called. The destructor
is only called for fully contructed objects.

So, if you do any non-managed allocation in the constructor you need to
use try/catch and cleanup the memory before exiting the constructor
(since your destructor will *not* be called in this case). Try to use
RAII objects like smart_ptr or auto_ptr to handle memory allocations
and deallocations automatically in case of exceptions.

But the nice thing is that all other objects - base object(s) and
aggregate objects will be destructed as expected.

/ Påhl

May 16 '06 #5
OK the derived class destructor will not be called because the derived
is not fully constructed, when the exception occurs.

But when I stepped through the debugger I see that the Base destructor
also is NOT called. (Also I have given a printf statement in the Base
class destructor to check this.)

May 16 '06 #6

Kannan wrote:
OK the derived class destructor will not be called because the derived
is not fully constructed, when the exception occurs.

But when I stepped through the debugger I see that the Base destructor
also is NOT called. (Also I have given a printf statement in the Base
class destructor to check this.)


and what is your compiler ?

May 16 '06 #7
Kannan posted:
OK the derived class destructor will not be called because the derived
is not fully constructed, when the exception occurs.

But when I stepped through the debugger I see that the Base destructor
also is NOT called. (Also I have given a printf statement in the Base
class destructor to check this.)

Did you compile and run the code I gave you elsethread?
Perhaps your degugger inlined the call to the destructor, and so it
appears that it isn't invoked when you step through -- I've seen that
before.
-Tomás
May 16 '06 #8
Disregard my last post.

I was using a MS ver 7.0 compiler and haven't turned on the exception
handler compiler option (/EHa or /EHs). Though the thrown object got
caught in the catch without turning on that option(s). However gcc got
it without giving any compiler options.

So what Påhl Melin said is right. The base class destructor got
called, when exception occurred in derived class constructor. That is
the destructor of all fully constructed objects will get called.

Sorry for the confusion. Thank you for the replies.

May 16 '06 #9

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

Similar topics

7
1901
by: Douglas Peterson | last post by:
Take a look at this code, it looks funny as its written to be as short as possible: -- code -- struct Base { ~Base() { *((char*)0) = 0; } }; struct Derived : public Base
11
1791
by: Dave | last post by:
try { ... } catch (exception e) { cout << e.what() << endl; } In the code above, e is caught by value rather than polymorphically (assume
3
3505
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? Is this because there could be an exception thrown when the code creates a std::exception? I would assume that is not the case. However, if I want to create a new exception class, derived from std::exception (say MyException) then how can I...
11
3827
by: Ivan A. | last post by:
Hi Is there any method to change System. Exception. Message property in derived exception's constructor? I need runtime class' information to initialize this property, but it's readonly. Thaks.
9
2181
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! http://www.geocities.com/jeff_louie/oop30.htm Regards, Jeff *** Sent via Developersdex http://www.developersdex.com ***
6
2501
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
3
2128
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think meets these requirements, but after reading the chapter on exceptions in 'Exceptional C++':Sutter, I am not sure if its is really Exception safe or Exception Neutral. I suppose putting the theory in that chapter into practice isn't trivial.
23
7240
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
10
2078
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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...
1
10168
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6785
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
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2924
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.