c++ - std::nothrow pretty much useless? -


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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -