Assume the following code:
Foo* p = new (std::nothrow) Foo();
\'p\' will equal 0 if we are out of heap memory.
What happens if we a
No, it won't be. The nothrow
only applies to the call to new
, not to the constructor.
I just tried it. The exception does get through. If you run the following code:
#include <new>
class Foo
{
public:
Foo()
{
throw 42;
}
};
int main()
{
Foo* foo = new(std::nothrow) Foo;
return 0;
}
then you get the following output (on Linux anyway):
terminate called after throwing an instance of 'int'
Aborted
So, the exception does indeed get through in spite of the nothrow.
Foo
's constructor can still throw exceptions and they will fall through.
The constructor is not called until after the memory is allocated.