Regarding nothrow and @safe

Jonathan M Davis jmdavisProg at gmx.com
Sat Aug 20 17:29:24 PDT 2011


On Saturday, August 20, 2011 16:41:01 Sean Eskapp wrote:
> Does nothrow mean the function itself does not through exceptions, or that
> the function body, as well as any called functions, do not throw? I wonder
> because allocating new memory inside a @safe nothrow function works, even
> though I'm used to "new" allocations throwing exceptions or Out-Of-Memory
> errors.

A nothrow function must not allow any Exceptions escape from it. So, it must 
either catch them all or it must only call functions which are nothrow and 
throw nothing itself.

An Error can still escape a nothrow function (though you may not be able to 
throw one manually, I don't know), because when an Error occurs, your program 
is pretty much toast. You're not normally supposed to try and catch those.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list