What is nothrow for?

Walter Bright newshound1 at digitalmars.com
Fri Apr 25 00:23:39 PDT 2008


Janice Caron wrote:
> So I guess my question is, in what circumstance would "nothrow" be
> helpful? And is it helpful /enough/ to warrant "polluting" all library
> code with "nothrow" annotations?

"nothrow" is, as you say, a contract. It specifies that a function must 
return normally (unless it aborts, crashes, or hangs). Such annotations:

1. improves the API documentation
2. enables significantly better code generation (if you use a lot of 
scope statements and struct destructors)
3. nothrow can be very useful in building up transactions, because you 
know that the components cannot fail
4. destructors cannot throw exceptions (because they are already in 
one). Andrei has proposed a method to deal with this, but it is as yet 
unimplemented.

For more info, see http://www.gotw.ca/gotw/082.htm and 
http://www.boost.org/community/exception_safety.html



More information about the Digitalmars-d mailing list