What does nothrow buy me?

Joseph Rushton Wakeling joseph.wakeling at webdrake.net
Tue Oct 27 10:49:14 UTC 2020


On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:
> Obviously, nothrow documents the behavior of a function and is 
> checked when the code is available to the compiler.
>
> At first glance, one could assume a nothrow function can save 
> the compiler from the need to generate exception handling stuff 
> for a function. But since nothrow doesn't mean the function 
> won't throw, but merely that it won't throw an Exception, what 
> optimizations does nothrow enable?

Another use-case is when you have functions or methods that are 
designed to be run as the entry point of a separate thread or 
fiber task.  An uncaught exception can cause the thread (or whole 
program) to hang, so having these functions guaranteed `nothrow` 
can be valuable.

Of course, this is a bit of a workaround for the lack of decent 
exception handling for these cross-thread/fiber cases, but that's 
a bigger technical discussion...


More information about the Digitalmars-d mailing list