473,473 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

constructor throw problem

hi all,

I have a constructor witch at a given time it throws an exception,
and i have an static object of that class,
witch is initialized into the cpp file

the problem is that when the exception is raised the
the application crashes ...

have any idea how to bypass this problem ...

have a nice day,
mihai

May 29 '06 #1
8 3206
mihai wrote:
hi all,

I have a constructor witch at a given time it throws an exception,
and i have an static object of that class,
witch is initialized into the cpp file

the problem is that when the exception is raised the
the application crashes ...

have any idea how to bypass this problem ...

have a nice day,
mihai


Can you post an example demonstrating your problem?

May 29 '06 #2
the code is someting like this ...

class A
{
public:
A()
{
if bad thing happens
throw exception;
}
....
};

class B
{
public:
static const A a;
};

// in B.cpp
const A B::a;

here is the problem ... :(
if A() throws the exception ... the application will creash...

May 29 '06 #3
dj
mihai wrote:
the code is someting like this ...

class A
{
public:
A()
{
if bad thing happens
throw exception;
}
...
};

class B
{
public:
static const A a;
};

// in B.cpp
const A B::a;

here is the problem ... :(
if A() throws the exception ... the application will creash...


Of course it will crash, since there is no way you can catch the
exception in global scope.
May 29 '06 #4
i understand this,

i am looking for a workaround ...

:)

have a nice day ...

May 29 '06 #5
Search for "Lazy initialization"

Basically is to defer the initialization of your object until you need it.
In your case, I'd use a pointer instead of your atribute

static A* a;

And initialize it with a NULL pointer.

You must give access to a through a getter method, returning a const
reference to a, instead of the pointer you have.

Protect your attribute inside the private section but let the getter in
the public section.

The code will be something like this:
class B {
static const A* a;
public:
static const A& getA() {
if(!a)
a=new A();
return *a;
}
};

// in B.cpp
const A* B::a=NULL;

mihai wrote:
the code is someting like this ...

class A
{
public:
A()
{
if bad thing happens
throw exception;
}
...
};

class B
{
public:
static const A a;
};

// in B.cpp
const A B::a;

here is the problem ... :(
if A() throws the exception ... the application will creash...

May 29 '06 #6
mihai wrote:
i understand this,

i am looking for a workaround ...


Well... don't throw an exception from the constructor.

May 29 '06 #7

"mihai" <Mi*********@gmail.com> skrev i meddelandet
news:11**********************@i40g2000cwc.googlegr oups.com...
i understand this,

i am looking for a workaround ...

:)

have a nice day ...


As you have probably already understood, there is just two ways to
solve this:

1. don't use a global or static variable
2. don't throw exceptions from the constructor
Your choice! :-)
Bo Persson
May 29 '06 #8
thank you ...
i'll chose one of this ...

have a nice day,
mihai

May 29 '06 #9

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

Similar topics

3
by: William Payne | last post by:
Hi, I have a class that allocates some memory dynamically in its (only) constructor. In my destructor I have a call to delete which corresponds to the new in the constructor. So far so good....
3
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? ...
21
by: mihai | last post by:
People say that is a bad technique to throw exception from constructors; and that the solution would be to create a function _create_ to initialize an object. What about copy constructors? How...
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
3
by: Martijn Mulder | last post by:
Is it good style to use a try...catch clause in the constructor or is it something that is frowned upon?
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
23
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.
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
3
by: vaclavpich | last post by:
Hi I've a question about constructors and exceptions. //************************************************************ class CObject { public: // ctor CObject(); // dtor ~ CObject();
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...
1
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...
0
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,...
0
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...
0
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...
0
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 ...
0
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...

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.