Error handling in contructors

B

balan

Hi
I am trying to allocate some memory inside a constructor.
The contructor can be invoked in context of nested object creation
(i.e object is member of another object ) or can be called
explicitly.
I want to know different ways to handle an error condition like when
memory alloc fails.
E.g
<<
Object::Object(int size){ array=new int[size] ; }

o = new Object(100);
If array == NULL, then how do i make 'o' as NULL?
or how do I check this condition?


balan
 
K

Kevin Goodsell

balan said:
Hi
I am trying to allocate some memory inside a constructor.
The contructor can be invoked in context of nested object creation
(i.e object is member of another object ) or can be called
explicitly.
I want to know different ways to handle an error condition...

Throw an exception.

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.16
like when
memory alloc fails.

In this case, you don't even need to throw your own exception. The
runtime system throws std::bad_alloc for you.
E.g
<<
Object::Object(int size){ array=new int[size] ; }

o = new Object(100);

This looks awful Java-ish. Why don't you just have

Object o(100);

?

Don't dynamically allocate unless you have to.
If array == NULL,

In standard C++, that cannot happen. If the 'new' operation fails, an
exception is thrown. Some compilers still incorrectly return 0, however.
then how do i make 'o' as NULL?

Object *o;

try
{
o = new Object(100);
}
catch (std::bad_alloc)
{
o = 0;
}

Or, using my earlier suggestion:

try
{
Object o(100);
// use o here
}
catch (...) // or use whatever exception you want
{
// handle error
}

-Kevin
 
J

Jon Bell

Hi
I am trying to allocate some memory inside a constructor. [...]
I want to know different ways to handle an error condition like when
memory alloc fails.
E.g
<<
Object::Object(int size){ array=new int[size] ; }

o = new Object(100);
If array == NULL, then how do i make 'o' as NULL?
or how do I check this condition?

If 'new' fails, it does not set array to NULL (at least not in standard
C++). Instead, it throws a bad_alloc exception, which you can catch at
some appropriate location in your program. A good C++ textbook
should discuss exception handling, with examples; or you might find
something useful with a Google search on "C++ exception handling".
 
J

Jonathan Mcdougall

If array == NULL,
In standard C++, that cannot happen. If the 'new' operation fails, an
exception is thrown. Some compilers still incorrectly return 0, however.

There is the nothrow specification defined in <new> :

# include <new>

int main()
{
int *p = new (nothrow) int;

if ( p == 0 )
{
// something bad happened
}
}


Jonathan
 
F

fabio

Kevin said:
balan said:
Hi
I am trying to allocate some memory inside a constructor.
The contructor can be invoked in context of nested object creation
(i.e object is member of another object ) or can be called
explicitly.
I want to know different ways to handle an error condition...

Throw an exception.

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.16
like when
memory alloc fails.

In this case, you don't even need to throw your own exception. The
runtime system throws std::bad_alloc for you.
E.g
<<
Object::Object(int size){ array=new int[size] ; }

o = new Object(100);

This looks awful Java-ish. Why don't you just have

Object o(100);

?

Don't dynamically allocate unless you have to.
If array == NULL,

In standard C++, that cannot happen. If the 'new' operation fails, an
exception is thrown. Some compilers still incorrectly return 0, however.
then how do i make 'o' as NULL?

Object *o;

try
{
o = new Object(100);
}
catch (std::bad_alloc)
{
o = 0;
}

Or, using my earlier suggestion:

try
{
Object o(100);
// use o here
}
catch (...) // or use whatever exception you want
{
// handle error
}

-Kevin


what you've written in your constructor is not a good programming style if
for example you'll also allocate some different resources.

if you had for example:

Object::Object(int size)
{
array1 = new int[size];
array2 = new int[size];
something_else();
}

~OObject::Object()
{
delete[] array2;
delete[] array1;
}

int main()
{
Object instance(10);
}

and array2 threw an exception, the constructor would fail and the memory
resource allocated for array1 would remain allocated, because the Object
instance is not yet created and destructor would not be called.

Anytime you need a resource, as written in the Stroustrup's book, make an
initialization (for example you could there use a std::vector).
 
B

balan

Hi
I am trying to allocate some memory inside a constructor. [...]
I want to know different ways to handle an error condition like when
memory alloc fails.
E.g
<<
Object::Object(int size){ array=new int[size] ; }

o = new Object(100);
If array == NULL, then how do i make 'o' as NULL?
or how do I check this condition?

If 'new' fails, it does not set array to NULL (at least not in standard
C++). Instead, it throws a bad_alloc exception, which you can catch at
some appropriate location in your program. A good C++ textbook
should discuss exception handling, with examples; or you might find
something useful with a Google search on "C++ exception handling".

THanks for all responses. I will have to read up on exception handling.

balan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top