What is the point of nothrow?

Bauss jj_1337 at live.dk
Tue Jun 12 15:48:58 UTC 2018


On Tuesday, 12 June 2018 at 14:19:42 UTC, Steven Schveighoffer 
wrote:
> On 6/10/18 7:59 PM, Bauss wrote:
>> What is the point of nothrow if it can only detect when 
>> Exception is thrown and not when Error is thrown?
>> 
>> It seems like the attribute is useless because you can't 
>> really use it as protection to write bugless, safe code since 
>> the nasty bugs will pass by just fine.
>
> Array invalid index throws Error, and asserts throw Error. I'm 
> not sure how much you could accomplish without such features. 
> In fact, I'd consider these ESSENTIAL to writing safe code.
>
> Bug-free code is a myth :)
>
> -Steve

Both are cases that the compiler __could__ warn you about 
potential possibility of them being thrown and thus allowing you 
to write code that makes sure it doesn't happen.

Ex.

int a = array[400];

Could yield a warning stating a possible a out of bounds error.

Where:

int a = array.length >= 401 ? array[400] : 0;

Wouldn't because you're handling the case.

What I'm trying to say it would be nice to catch certain 
situations like that (of course not possible with all) because 
you'll end up having to handle them anyway after the error is 
thrown.






More information about the Digitalmars-d-learn mailing list