c++ - std::nothrow pretty much useless? -
this question has answer here:
nothrow constant: constant value used argument operator new , operator new[] indicate these functions shall not throw exception on failure, return null pointer instead.
but here in simple example, instead of returning null, new
throws exception:
struct somestruct { somestruct() { std::bad_alloc exception; throw exception; } }; int _tmain(int argc, _tchar* argv[]) { somestruct* somestruct; somestruct = new (std::nothrow) somestruct; return 0; }
any explanation please? indicates, despite having (std::nothrow)
parameter new
still need put code in try...catch
block.
std::nothrow
means instead of std::bad_alloc
being thrown on failure acquire memory, new
expression returns nullptr
. not mean new
expression not ever throw.
if somestruct
did not throw in constructor, new
expression never throw.
Comments
Post a Comment