DIP33: A standard exception hierarchy

Ali Çehreli acehreli at yahoo.com
Thu Apr 4 12:16:01 PDT 2013


On 04/04/2013 08:47 AM, Jesse Phillips wrote:

 > On Wednesday, 3 April 2013 at 16:19:25 UTC, Ali Çehreli wrote:
 >> >      auto myFile = "some.tmp";
 >> >      scope(exit) remove(myFile);
 >> >
 >> >      // setup code here
 >> >      manipulateFileRange(range);
 >>
 >> We are in agreement that it would be impossible to prove one way or
 >> the other whether removing the file would be the right thing to do or
 >> whether it will succeed.
 >
 > All you need is one example where it would remove the wrong file,

$ dmd deneme.d -ofdeneme -I~/deneme/d -O -inline -m32
$ ./deneme

import std.stdio;
import std.string;
import std.array;

void main()
{
     auto myFile = "some.tmp";
     scope(exit) writeln(format("removing %s", myFile));

     writeln("myFile.ptr ", myFile.ptr);

     void manipulateElement(E)(ref E e)
     {
         size_t local;
         // Playing with pointers (BUG HERE)
         *(&local + 10) = 4;
         *(&local - 1) = 100;
         writeln(&local - 1);
         writeln("myFile ", &myFile);
         writeln("e ", e.ptr);
     }

     void manipulateFileRange(R)(R range)
     {
         for (size_t i = 0; i != range.length; ++i) {
             writeln("&i ", &i);
             writeln("i ", i);
             manipulateElement(range[i]);
         }
     }

     manipulateFileRange([ myFile ]);
}

Note that RangeError below is caused by a bug in the program. Once that 
happens, we cannot say anything about the state of the program. It may 
be 99% correct but it is still in an invalid state.

Here is the output of the program (arrow and comment are added manually 
by me):

myFile.ptr 806C0C4
&i FFFCE5DC
i 0
FFFCE5DC
myFile FFFCE608
e 806C0C4
&i FFFCE5DC
i 101
removing some  <-- WRONG FILE! (not "some.tmp")
core.exception.RangeError at deneme(125887): Range violation

 > I just
 > requested that it have higher accuracy than Exception since what you're
 > claiming as invalid state is the same invalid state exceptions check for
 > (I didn't expect this).

Unfortunately, exception is too general a term and unfortunately both 
Exception and Error use the same mechanism in D.

A thrown Exception does *not* indicate invalid program state; Error 
does. A thrown Exception means that some task could not be accomplished.

Error is different: It means that an assertion failed. An assert failure 
means that the fundamental truths that the programmer has built the 
program on has been shattered. As simple as that. The runtime cannot 
assess whether the program is 1% or 100% correct. The only sensible 
thing to do is to stop executing so that no more harm is done. Again, a 
failed assert means that the program has gone out of line. It did 
something wrong. It is in an invalid state.

Ali



More information about the Digitalmars-d mailing list