473,385 Members | 1,593 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,385 software developers and data experts.

Exception Destructor on new

Hi,

I am learning C++ and need export advice on the below program.
I have read it that whenever a exception is genrated and control
enteres the catch block it will call destructors for all the
successfuly completed constructors. But in the below case
i am doing :-
base *ptr=new base;
throw 44;
But the destructor for base is not called once it enters the catch
block. Please let me know why it is so and how to come out of it.

#include<iostream>
#include<exception>
using namespace std;
class base
{
public:
base() { cout<<"Constructor"<<endl; }
~base() { cout<<"Destructor"<<endl; }
};
int main()
{
try {
base *ptr=new base;
throw 44;
}catch(...) { // I am
expecting base destructor to be called here
cout<<" Inside catch"<<endl; // But it is not
}
}

Mar 23 '07 #1
4 1986
* Subra:
Hi,

I am learning C++ and need export advice on the below program.
I have read it that whenever a exception is genrated and control
enteres the catch block it will call destructors for all the
successfuly completed constructors. But in the below case
i am doing :-
base *ptr=new base;
throw 44;
But the destructor for base is not called once it enters the catch
block. Please let me know why it is so and how to come out of it.
The pointer is "destroyed", in a sense.

The object it points to is not.

To have the pointed to object destroyed along with the pointer, use a
smart pointer such as std::auto_ptr.
#include<iostream>
#include<exception>
You don't need the <exceptionheader, you're not using anything from
that header.

To use std::auto_ptr you need

#include <memory>

using namespace std;
class base
{
public:
base() { cout<<"Constructor"<<endl; }
~base() { cout<<"Destructor"<<endl; }
};
int main()
{
try {
base *ptr=new base;
Make that

std::auto_ptr<baseptr( new base );

throw 44;
}catch(...) { // I am
expecting base destructor to be called here
cout<<" Inside catch"<<endl; // But it is not
}
}
Hth.,

- Alf (an export on C++)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 23 '07 #2
Another approach is

int main()
{
base *ptr = NULL;
try
{
ptr=new base;
throw 44;
}
catch(...)
{
if(p)
delete ptr;
cout<<" Inside catch"<<endl;
}

Mar 23 '07 #3
Subra <ma*********@gmail.comwrote:
Hi,

I am learning C++ and need export advice on the below program.
I have read it that whenever a exception is genrated and control
enteres the catch block it will call destructors for all the
successfuly completed constructors. But in the below case
i am doing :-
base *ptr=new base;
throw 44;
But the destructor for base is not called once it enters the catch
block. Please let me know why it is so and how to come out of it.

#include<iostream>
#include<exception>
using namespace std;
class base
{
public:
base() { cout<<"Constructor"<<endl; }
~base() { cout<<"Destructor"<<endl; }
};
int main()
{
try {
base *ptr=new base;
throw 44;
}catch(...) { // I am
expecting base destructor to be called here
cout<<" Inside catch"<<endl; // But it is not
}
}
Alf Steinbach has given you one suggestion. However, if you do not need
to allocate base dynamically, then observe this program:

#include <iostream>

class Base {
public:
Base() { std::cout << "Constructor\n"; }
~Base() { std::cout << "Destructor\n"; }
};

int main()
{
try {
Base b;
throw 44;
}
catch (...) {
std::cout << "Inside catch\n";
}
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 23 '07 #4
"Subra" <ma*********@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
Hi,

I am learning C++ and need export advice on the below program.
I have read it that whenever a exception is genrated and control
enteres the catch block it will call destructors for all the
successfuly completed constructors. But in the below case
i am doing :-
base *ptr=new base;
throw 44;
But the destructor for base is not called once it enters the catch
block. Please let me know why it is so and how to come out of it.

#include<iostream>
#include<exception>
using namespace std;
class base
{
public:
base() { cout<<"Constructor"<<endl; }
~base() { cout<<"Destructor"<<endl; }
};
int main()
{
try {
base *ptr=new base;
You are constructing a base pointer. With new it calls a base constructor.
throw 44;
Even without this throw, the destructor for base isn't going to be called,
because you never did delete ptr.
}catch(...) { // I am
expecting base destructor to be called here
cout<<" Inside catch"<<endl; // But it is not
}
The base pointer is destroyed. But, if you call new, you have to call
delete. Which you haven't.

Objects instantized with new never have their destructors called unless you
call delete (there may be exceptions, but this is the general rule). If you
want your base instance destructor called (and you do) then you have to call
delete ptr.
}

Mar 23 '07 #5

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

Similar topics

7
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
16
by: Timothy Madden | last post by:
Hy I have destructors that do some functional work in the program flow. The problem is destructors should only be used for clean-up, because exceptions might rise at any time, and destructors...
8
by: JAL | last post by:
According to MSDN2, if a managed class throws an exception in the constructor, the destructor will be called. If an exception is thrown in the constructor the object never existed so how in the...
10
by: junw2000 | last post by:
Hi, Below is a simple code about exception. #include <iostream> using namespace std; struct E { const char* message; E(const char* arg) : message(arg) { } };
3
by: George2 | last post by:
Hello everyone, Suppose I have some objects created on local function stack (not on heap). And I allocate and free all the related resources in the constructor and destructor (e.g. memory and...
5
by: Vijay | last post by:
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";}
4
by: amphetaman | last post by:
If I derive my own Exception class from std::runtime_error, do I have to write the destructor even if its body is empty? #include <stdexcept> class Exception : public std::runtime_error {...
11
by: Peter Jansson | last post by:
Dear newsgroup, In the following code, it looks as though the exception thrown in the constructor is not caught properly. I get this output: ---- standard output ---- Base() constructor....
16
by: john6630 | last post by:
Coming from the .Net world, I am used to the try...catch...finally approach to error handling. And PHP 5 now supports this approach. But I am not clear what happens to unhandled errors/exceptioins?...
6
by: asm23 | last post by:
Hi, everyone, I'm learning <<thinking c++>volume Two, and testing the code below. ///////////////////////////////////////////////////////////////////////////// //: C01:Wrapped.cpp #include...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.