On 05/31/2017 02:41 PM, Ola Fosheim Grøstad wrote:
> On Wednesday, 31 May 2017 at 21:30:05 UTC, Ali Çehreli wrote:
>> How could an Exception work in this case? Catch it and repeat the same
>> bug over and over again? What would the program be achieving? (I
>> assume the exception handler will not arbitrarily decrease index values.)
>
> How is this different from a file system exception?
> The file system is memory too...

When you say "memory" I think you refer to the thought of bounds checking being for prevention of memory corruption. True, memory corruption can happen when the program writes out of bounds but it's one special case. The actual reason for bounds checking is maintaining an invariant.

Regarding the file system, because it's part of the environment of the program, hence the program cannot control, it's correct to throw an Exception, in which case the response can be "Cannot open that file; how about another one?".

In the case of array indexes, they are in complete control of the program, hence a bug when out of bounds. It's not possible to say "Bad index; let me try 42 less."

Ali

Reply via email to