A good way of handling errors from a constructor?

Started by
9 comments, last by Peter Svensson 23 years, 9 months ago
quote:
they are referring to the STL overloaded new operator which throws an exception. VC 6.0 SP2 still does not properly support that overload


STL doesn''t have its own operator new (STL is the collective name for the containers, algorithms, iterators & function objects (did I miss anything?)), it''s the standard C++ operator new that throws std::bad_alloc, so you probably wont find the source code it (the compiler should do it by default). new in MSVC isn''t ''correct'' because it still returns NULL (but new in MFC throws CMemoryException).

If your going to write your own operator new to use malloc, you should do the same for delete to call free. An easier would be to leave the current new and delete alone, and write your own new handler (it''ll be called if new fails). e.g.

#include int my_new_handler(size_t size){	throw std::bad_alloc();}int main(int argc, char* argv[]){	_set_new_handler(my_new_handler);	//now if new fails it''ll throw std::bad_alloc	return 0;} 

This topic is closed to new replies.

Advertisement