Re: operator new not forced to throw std::bad_alloc?
numerical.simulation@web.de ha scritto:
Hi!
During some debug session of an MFC application (VS2005) I found out
that std::allocator<double>::allocate calls operator new which throws
a
CMemoryException.
I expected operator new to throw std::bad_alloc, so that exception
went
uncaught. This is odd.
void* __cdecl operator new(size_t nSize)
{
...
// That's platform specific code here:
if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0)
}
Q: Did I misunderstand Stroustrup?
No.
Q: Is an STL allowed to throw things which do not inherit from
std::exception?
It depends. The standard lists all cases where an exception must be
thrown and in that cases which exception is thrown. In all such cases,
the exception derives from std::exception. However, whenever the library
code calls user-provided code (and that includes replacement functions),
the library can let the original exception pass or can catch it and
rethrow it even if it doesn't derive from std::exception. Moreover, if I
recall correctly, some components of the iostreams may fail by throwing
an unspecified exception.
Q: No standard under my pillow: What's the wording about what operator
new may throw?
18.4.1.1 [lib.new.delete.single], about
void* operator new(std::size_t size) throw(std::bad_alloc);
states: "Required behavior: Return a non-null pointer to suitably
aligned storage (3.7.3), or else throw a bad_alloc exception. This
requirement is binding on a replacement version of this function."
So the library your are working with is definitely non-conforming.
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]