This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[970929] Can't throw in constructor (fwd)


Something else my test shows: Even when it "runs" per H.J.'s
experience, it doesn't do the thing that the test was designed to
show: When a constructor in a new expression throws an exception, the
corresponding operator delete should be called to reclaim the memory
used by the not-created-after-all object.

> -----------------------------------------------------------------------
> #include <iostream.h>
> #include <sys/types.h>
> 
> 
> char x[100];
> class Noisy {
>   public:
>     Noisy() { throw "Aaaaah!"; }
> 
>     void * operator new (size_t)
> 	{ cout << "Noisy::new" << endl; return x; }
>     void operator delete (void *, size_t)
> 	{ cout << "Noisy::delete" << endl; }
> };
> 
> void
> foo()
> {
>     Noisy *p = 0;
>     try {
> 	p = new Noisy;
> 	delete p;
>     }
>     catch (...) {
> 	cout << "Can't be noisy" << endl;
>     }
> }
> 
> main()
> {
>     foo();
> }
> -----------------------------------------------------------------------


-- 
Chip Salzenberg          - a.k.a. -           <chip@pobox.com>
        "He's Mr. Big of 'Big And Tall' fame."  // MST3K


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]