[Issue 583] New: Throw statements in contracts

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 21 18:23:53 PST 2006


http://d.puremagic.com/issues/show_bug.cgi?id=583

           Summary: Throw statements in contracts
           Product: D
           Version: 0.174
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: daekharel at gmail.com


void foo()
in { throw new Exception("some exception"); }
body {}

-----

void foo()
in { exceptionThrower(); }
body {}

void exceptionThrower() { throw new Exception("some exception"); }

-----

Obviously, the two pieces of code above do essentially the same thing. However,
compiling the first produces the error: "Error: throw statements cannot be in
contracts", whereas the latter compiles without error and runs as would be
expected.

The exclusion of error/exception throwing from contracts is therefore
inconsistent (since you can call code that throws errors), and I'm not sure
that it's really all that useful. Consider, after all, that D itself does
essentially the equivalent of throwing errors in contracts when you use arrays.
Normally, D throws an ArrayBoundsError when you try to access an array index
outside the array's bounds; however, when compiled with the "-release" switch,
it throws no such error, and gives you access to whatever would be in the spot
you're trying to access if the array extended that far (assuming you can access
that memory locaiton at all, that is).

If D itself (or at least the DMD implementation) can throw errors other than
AssertError in contracts, why shouldn't programmers be able to, especially
given that there's an easy workaround?


-- 




More information about the Digitalmars-d-bugs mailing list