Will delete operator throw any exception?

S

siddhu

What happens when deletion falis?Does operator delete function throw
exception?
I assume that nothrow is not available with delete. Or is it available?
 
G

Gianni Mariani

siddhu said:
What happens when deletion falis?Does operator delete function throw
exception?
I assume that nothrow is not available with delete. Or is it available?

Nothing stops delete from thowing, however, don't ever do it.

Bad things happen when delete throws.
 
S

siddhu

Nothing stops delete from thowing, however, don't ever do it.

Bad things happen when delete throws.

Does operator delete function which we get from compiler has any
exception specification?
 
R

Robert Bauck Hamar

siddhu said:
Does operator delete function which we get from compiler has any
exception specification?

Yes: throw(). Note however that this doesn't mean destructors can't throw.
So a delete statement can still throw with the builtin operator delete().
 
J

James Kanze

What happens when deletion falis?Does operator delete function throw
exception?
I assume that nothrow is not available with delete. Or is it available?

By definition, a deletion cannot fail. No matter what happens
in it, the object doesn't exist after deletion, so deletion
hasn't failed.

There are a (very) few patterns which count on a destructor
throwing an exception. In every case, however, the object which
does this is constructed in place of a throw statement, because
an error has been detected, and should only be used as a
temporary, and never be the object of a delete (nor, in fact,
have a lifetime long enough for anything else to be able to
raise an exception while it lives).

And the operator delete() function has an empty exception
specification, and so is not allowed to throw an exception.
 
R

Ron Natalie

Gianni said:
Nothing stops delete from thowing, however, don't ever do it.

Bad things happen when delete throws.
I can't imagine why the deallocation would throw (barring previous
undefined behavior). However, the destructor could throw an exception.
That however as stated above is a BAD idea. There are places where
a destructor throw would not be catchable which is bad.
 

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,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top