nothrow and exceptions

D

Dave

Dave said:
Is it possible for the nothrow form of the new operator to ever throw?

Oh Lord, what on Earth is wrong with me...

Is it possible for *PLACEMENT* new to ever throw???

Somebody shoot me!!!
 
R

Ron Natalie

Dave said:
Is it possible for the nothrow form of the new operator to ever throw?

No...it has a throw() exception specification on it. If something inside it were to
throw, terminate() would be called.
 
R

Ron Natalie

Dave said:
Is it possible for *PLACEMENT* new to ever throw???

Sure, the constructor for the object can throw. But the placment allocation
function can not throw. It's defined with a throw() exception specification.
Terminate() would be called even if the the allocation function was replaced
with something that would throw.
Somebody shoot me!!!

Bang.
 
H

Howard Hinnant

Dave said:
Oh Lord, what on Earth is wrong with me...

Is it possible for *PLACEMENT* new to ever throw???

Somebody shoot me!!!

<chuckle> Actually your original question has interesting answers. Just
goes to show: there are no dumb questions! :)

Yes, the nothrow form of the new operator can throw. Well, that's
probably a bit too inflamatory. A new-nothrow expression can throw.
Consider:

struct X
{
X() {throw "up";}
};

void foo()
{
X* xp = new (std::nothrow) X;
}

The new expression in foo() will throw, even though it is using the
nothrow expression. Same deal for placement new.

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top