What does nothrow buy me?

Steven Schveighoffer schveiguy at gmail.com
Mon Nov 9 15:55:44 UTC 2020

On 11/8/20 6:53 PM, H. S. Teoh wrote:
> On Sun, Nov 08, 2020 at 10:00:28PM +0000, random via Digitalmars-d wrote: > [...]
>> When you call functions which potentially throw, the compiler has to
>> generate landing pads to handle exceptions if:
>> 1. You have a catch block.
>> 2. You have to call destructors.
>> This does not directly impact runtime performance but it bloats the
>> executable code (and it will probably impact runtime performance
>> because of caching).
> [...]
> Nothrow functions can have exception handling code elided. So in that
> case it will not incur the runtime cost.

I think this is slightly incorrect. If you throw an Error inside a 
nothrow function, it will properly run destructors, and you can catch 
the Error inside the function.

I would say as well, if you catch an Error or Throwable resulting from a 
call to a nothrow function, it should always generate that handling 
code. But I think the spec is written such that it might elide the 
exception handling code (I don't see this ever happening).

The main case is when you *don't* catch Error or Exception when calling 
a nothrow function, and your code does cleanup via RAII, the cleanup 
will not happen.


More information about the Digitalmars-d mailing list