3 ms·
Array bounds errors in D are treated as "go directly to jail, do not pass Go do not collect $200" errors. I.e. they are not recoverable, so no need for error h
by WalterBright 3y ago
Array bounds errors in D are treated as "go directly to jail, do not pass Go do not collect $200" errors.
I.e. they are not recoverable, so no need for error handling.
Many people have debated this and wanted such errors to be recoverable, but the principle in play is that the program is in an undefined state if there are such errors, and should not be allowed to continue as we wouldn't want it to launch nuclear missiles.
- woodruffw 3y agoThat’s (IMO) the right way to do it, but it’s also a harder pill to swallow from the C standards side: functions that die as a side effect rather than being UB are less precedented, have to be taught differently, etc. C’s lack of iterators also means that indexing bugs are more likely in idiomatic C programs than they are in equivalent C++ or D programs. This is another place where the standards body should improve things, but it also means that they’re somewhat constrained when it comes to slice proposals: making every index be potentially fatal is less palatable when your programming language doesn’t divert people away from indexing as much as it could.
- CodesInChaos 3y agoSince dying is a valid implementation of UB, I don't see a problem. It's definitely better than memory corruption. In general my preferred way to handle OOB is an unchecked exception (like in Rust or C#), but that's not really an option C.