Acces Violation: assert with null instance

Bradley Smith digitalmars-com at baysmith.com
Fri Jan 26 01:01:51 PST 2007


Walter Bright wrote:
> You can catch access errors by catching Exception, at least under 
> Windows, because I haven't figured out how to do it under Linux.

I know. That is not the point. The point is that evaluation of a null 
reference is inconsistent. For an if statement, null is evaluated as 
false, but for an assert it is an access violation. See code below.

Conceptually, assert (expression) is nothing more that "if (!expression) 
throw new AssertError;". However, it doesn't act that way.

import std.stdio;
class Class {}
void main() {
   Class c;
   if (!c) {
     writefln("c is false?");
   }
   assert(c);
}


More information about the Digitalmars-d-learn mailing list