Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can new still throw an exception?

I’m a new C++ programmer, so I never wrote C++ code for anything older than C++11. I was reading Scott Meyers "Effective C++, 2nd Edition" (I know it is old, but I think it still has some valid points).

In the book, according to "Item 7", new can throw exceptions which should be handled.

Can new still throw exceptions?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Should I be prepared about exceptions that could be thrown by a smart pointer or new?

>Solution :

Yes, on allocation new can throw a bad_alloc exception.

That is, unless you pass const std::nothrow_t& as the second parameter, where you’ll be guaranteed a return value of nullptr

See details here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading