Topic: Raising Exception in the Constructor


Author: Pete Becker <petebecker@acm.org>
Date: 1997/08/08
Raw View
Srinivas Vobilisetti wrote:
>
> Tony Minkoff wrote:
>  >
>  > In article <5pdp7q$stf@netlab.cs.rpi.edu> you write:
>  > >In the following code, I have class X with a constructor which just
>  > >throws an exception. In the main function, I am creating an instance of
>  > >X on the heap. As the X constructor throws an exception, I am expecting
>  > >the new operator to return a 0(NULL) value. I am assuming that the
>  > >compiler whould return the memory to the heap (to avoid memory leak)
>  > >when the constructor fails and return a 0 (NULL). I am not sure how far
>  > >my assumption is correct.
>  > >
>  > >Thanks,
>  > >Srinivas
>  > >
>  > >
>  > >#include <iostream.h>
>  > >
>  > >class X {
>  > >
>  > >   public:
>  > >
>  > >      X() { throw 2; }
>  > >};
>  > >
>  > >int main()
>  > >{
>  > >   X *px; // intentionally left uninialized
>  > >   try {
>  > >      px = new X; // what should px be?
>  > >   }
>  > >   catch (int i) {
>  > >      if(px == 0) cout << "px set to zero\n";
>  > >      else cout << "px not set to zero\n";
>  > >   }
>  > >
>  > >   return 0;
>  > >}
>  > >
>  > >output on my compiler:
>  > >
>  > >px not set to zero
>  >
>  > new does not return zero when the contstructor throws; actually, new
>  > never returns at all.  Control is transfered directly from the throw
>  > statement to the catch block.  Hence the assignment to px, which normally
>  > occurs after the return from new, never occurs.  px therefore retains
>  > its previous value, which can be anything, since it was never
initialized.
>
> Thanks for the information. Even thats what I understand from DWP 96.
> But the clause 5.3.4.13 says the allocation function shall either
> return  NULL or address to a block of memory of size requested. Also, it
> says that the allocation function can indicate failure by throwing
> bad_alloc exception. So, does this mean, if allocation function throws a
> bad_alloc exception, the pointer variable will be set to NULL and then
> propagate the exception but if the constructor throws an exception, the
> exception will be propagated without setting the pointer variable to
> NULL?

Paragraph 13 begins with the words "The allocation function". In order
to know what this means, you need to look at paragraph 9, which, in
turn, refers you to clause 3.7.3.1. That clause contains the full
requirements on what the allocation function does. There are "no-throw"
allocation functions, which return NULL. All other allocation functions
must throw bad_alloc if they are unable to allocate memory. The only way
to invoke a no-throw allocation function is with an explicit no-throw
new invocation:

 new(nothrow)foo;

In the absence of nothrow, new throws an exception when it cannot
allocate memory.
 In any case, if an exception is thrown prior to assigning a value to
the pointer, the assignment will not occur. That happens when new is
unable to allocate memory or when the constructor throws an exception.
 -- Pete

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "(C) Chichiang Wan" <wanc@hpclear6.cup.hp.com>
Date: 1997/07/04
Raw View
Srinivas Vobilisetti wrote:
 >

 >
 > Thanks for the information. Even thats what I understand from DWP 96.
 > But the clause 5.3.4.13 says the allocation function shall either
 > return  NULL or address to a block of memory of size requested. Also, it
 > says that the allocation function can indicate failure by throwing
 > bad_alloc exception. So, does this mean, if allocation function throws a
 > bad_alloc exception, the pointer variable will be set to NULL and then
 > propagate the exception but if the constructor throws an exception, the
 > exception will be propagated without setting the pointer variable to
 > NULL?
 >

allocation function means the operator new.  The one you can overload.
Not the new expression.

--
**********************************************************************
Name: Chichiang Wan
email: wanc@cup.hp.com
Phone: (408) 447-5762
**********************************************************************
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Srinivas Vobilisetti <Srinivas.Vobilisetti@mci.com>
Date: 1997/07/03
Raw View
Tony Minkoff wrote:
 >
 > In article <5pdp7q$stf@netlab.cs.rpi.edu> you write:
 > >In the following code, I have class X with a constructor which just
 > >throws an exception. In the main function, I am creating an instance of
 > >X on the heap. As the X constructor throws an exception, I am expecting
 > >the new operator to return a 0(NULL) value. I am assuming that the
 > >compiler whould return the memory to the heap (to avoid memory leak)
 > >when the constructor fails and return a 0 (NULL). I am not sure how far
 > >my assumption is correct.
 > >
 > >Thanks,
 > >Srinivas
 > >
 > >
 > >#include <iostream.h>
 > >
 > >class X {
 > >
 > >   public:
 > >
 > >      X() { throw 2; }
 > >};
 > >
 > >int main()
 > >{
 > >   X *px; // intentionally left uninialized
 > >   try {
 > >      px = new X; // what should px be?
 > >   }
 > >   catch (int i) {
 > >      if(px == 0) cout << "px set to zero\n";
 > >      else cout << "px not set to zero\n";
 > >   }
 > >
 > >   return 0;
 > >}
 > >
 > >output on my compiler:
 > >
 > >px not set to zero
 >
 > new does not return zero when the contstructor throws; actually, new
 > never returns at all.  Control is transfered directly from the throw
 > statement to the catch block.  Hence the assignment to px, which normally
 > occurs after the return from new, never occurs.  px therefore retains
 > its previous value, which can be anything, since it was never initialized.

Thanks for the information. Even thats what I understand from DWP 96.
But the clause 5.3.4.13 says the allocation function shall either
return  NULL or address to a block of memory of size requested. Also, it
says that the allocation function can indicate failure by throwing
bad_alloc exception. So, does this mean, if allocation function throws a
bad_alloc exception, the pointer variable will be set to NULL and then
propagate the exception but if the constructor throws an exception, the
exception will be propagated without setting the pointer variable to
NULL?

Srinivas
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]