1

Consider the following code snippet:

try{
LibObject* obj = new LibObject();
if (!obj)
    return 0;
}catch(...){
    return 0;
}
return 1;

I don't know the implementation of the LibObject since it comes from an external library.

Generally speaking, could have any sense to check if the new object instance (obj) is NULL? Or the check is simply unuseful?

Could a new statement return a NULL object without throwing an exception?

ABCplus
  • 3,981
  • 3
  • 27
  • 43
  • 1
    possible duplicate of [Will new return NULL in any case?](http://stackoverflow.com/questions/550451/will-new-return-null-in-any-case) – user4815162342 Feb 25 '15 at 11:29

1 Answers1

1
LibObject* obj = new LibObject(); 

Don't use parenthesis here. And if you don't want alloc exception, you should use std::nothrow

LibObject* obj = new(std::nothrow) LibObject;
if (obj == nullptr) return 0;
else return 1;

This is the correct syntax. You can find a reference here:

http://www.cplusplus.com/reference/new/nothrow/

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
amchacon
  • 1,891
  • 1
  • 18
  • 29