Is the C++ Exception object created on the heap?

I write the code like the following,

        // Note that the output object below functions like the C++'s cout. It's my own
        // implementation of displaying program information and it's insignificant here.
        try{
                try{
                        //output<<"exhausting the heap"<<endl;
                        int* p = new int();
                                while(p!=NULL){
                                p = new int();
                                }
                        //output<<" heap exhausted"<<endl;
                }
                catch(...){
                        // never reaches here because I use the --force_new_nothrow
                        // Even if I dont't use --force_new_nothrow, the program never
                        // reaches here too...
                        //output<<"heap exhausted. and received an exception"<<endl;
                }

                //output<<"now I throw an int"<<endl;
                throw int(); 

        }
        catch(...){
                // unfortunately, the program never reach here
                // I guess it's because the exception object is to be created on the heep
                // but the heap is already exhausted.
                // So how should I handle the exception when allocation fails occurs?

                // output some debug imformation
                //output<<"unkown exception"<<endl;
        }
        // never reaches here too...
        //output<<"end"<<endl;
        while(1);


Actually the originan code I wrote is more complated. The Exception obj I throw is a class, so is the allocation object type I use to exhaust the heap. I output the address of these object, under the conditions of exhausted heap and unexhausted respectively. I found there is an exception object created on the heap.

So could anyone tell me how to handle the exception when memory allocation fails occurs ?

Parents Reply Children
No data
More questions in this forum