Comparing Exceptions and Errors

Ali Çehreli acehreli at yahoo.com
Sun Jun 5 14:30:30 UTC 2022


On 6/5/22 04:43, kdevel wrote:
 > On Sunday, 5 June 2022 at 00:40:26 UTC, Ali Çehreli wrote:
 > [...]
 >> Errors are thrown when the program is discovered to be in an invalid
 >> state.
 >
 > The following program throws an `Error` in popFront:
 >
 >     import std.range;
 >
 >     void main ()
 >     {
 >        int [1] a;
 >        auto q = a[1..$]; // line 6
 >        q.popFront;       // line 7, throws core.exception.AssertError
 >     }
 >
 > When the program counter (PC) is at line 6 the program is in a valid 
state.
 >
 > At no time the program is in an invalid state and it would not pass into
 > an invalid state if popFront threw an `Exception` instead of an `Error`.

That looks like an argument for changing the behavior of D runtime when 
an out-of-bounds access occurs. Of course, it is too late to change that 
at this time but perhaps there can be a compiler switch like 
-boundstype=[error|exception].

The programmer has many options to prove that nothing crazy is 
happening. A common example suitable for many cases:

   enforce(!q.empty); // (Throws Exception)
   q.popFront;

Ali



More information about the Digitalmars-d-learn mailing list