What does nothrow buy me?

Steven Schveighoffer schveiguy at gmail.com
Wed Oct 28 14:46:40 UTC 2020


On 10/28/20 8:52 AM, Atila Neves wrote:
> On Tuesday, 27 October 2020 at 18:21:23 UTC, H. S. Teoh wrote:
>> On Tue, Oct 27, 2020 at 06:10:45PM +0000, Atila Neves via 
>> Digitalmars-d wrote:
>>> On Monday, 26 October 2020 at 18:47:41 UTC, Q. Schroll wrote:
>> [...]
>>> > 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?
>>>
>>> It's also extremely useful for when D code is being called from other 
>>> languages. Throwing an exception from a function registered with 
>>> Python is not a thing one wants to do, for instance.
>>
>> But how does it help in the case of throwing an Error from a function 
>> registered with Python?  Arguably, that's something you even *more* 
>> wouldn't want to do.
>>
>>
>> T
> 
> Unfortunately, it doesn't. But errors are supposed to mean a 
> non-recoverable problem anyway. I know it's not always like that in 
> practice and that you don't want to crash someone's Excel sheet, but... 
> making them nothrow makes me write a catch block, which makes me also 
> catch errors.

How does it make you write a catch block? If you only call nothrow 
functions, then your function is nothrow. If you assert in your nothrow 
function, it won't complain about catching.

How does it make you write a catch block that catches Errors? Catching 
Exception is sufficient to shut up the compiler.

-Steve


More information about the Digitalmars-d mailing list