473,545 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ 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 3208
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*********@gm ail.com> skrev i meddelandet
news:11******** **************@ i40g2000cwc.goo glegroups.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
1319
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. Anyway, after I've allocated the memory that I need in my constructor I proceed to call a function in a third-party library. If that function fails, I...
3
3484
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...
21
4392
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 can we avoid throwing exceptions? If we already have an abject witch was initialized wit _create_ we will be forced to call create in copy...
12
2657
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 exception instead. If I step into __CxxThrowException in debug/assembly mode, it looks like the pThrowInfo parameter created by the compiler excluded...
3
10821
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
36065
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 object. TIA Sek
23
7210
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
3940
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 is much appreciated. JD
12
7175
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? class Trial { public: Trial() {
3
2761
by: vaclavpich | last post by:
Hi I've a question about constructors and exceptions. //************************************************************ class CObject { public: // ctor CObject(); // dtor ~ CObject();
0
7475
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...
0
7664
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. ...
0
7766
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...
0
5981
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...
0
3463
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...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1897
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
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
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...

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.