www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 583] New: Throw statements in contracts

reply d-bugmail puremagic.com writes:

           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 digitalmars.com
        ReportedBy: daekharel 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

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?

Nov 21 2006
parent d-bugmail puremagic.com writes:

bugzilla digitalmars.com changed:

           What    |Removed                     |Added
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID

Since D is a systems programming language, you can always find a way to defeat
the compiler's checking. The reason that throwing arbitrary exceptions is not
allowed in contracts is because the sole purpose of the code in the contracts
is to check for programming bugs, and throwing non-assert exceptions is not
consistent with that.

Jun 29 2008