Contracts + Assert

Mike Linford sgtmuffles at myrealbox.com
Tue Oct 30 20:43:25 PDT 2007


So in what cases would one prefer to use contracts over normal exception handling?

Ary Borenszweig Wrote:

> Mike Linford escribió:
> > I'm a bit confused about why it seems to be advocated that only assert()'s be used in contracts. Why is it that more specific exceptions aren't thrown instead? Wouldn't this make debugging easier?
> 
> Contracts are a way to ensure that the program is working as expected, 
> and it is used as expected.
> 
> For example, if an assert in an "in { }" section fails, that means the 
> some wrong parameter was passed to the function. It was a programmer's 
> error: either she mistyped something, or she don't understand it's 
> usage. If the "out { }" fails, the function's logic is wrong (or the 
> "out" :-P).
> 
> For an invariant, if it fails, it's a problem in the class' logic.
> 
> If I remember correctly, assertion failures do throw an AssertError 
> exception, but these exceptions aren't supposed to be catched (if the 
> program's logic is wrong, the only way to recover from that exception is 
> by changing the program itself). In fact, contracts are removed at all 
> if you compile in -release mode.



More information about the Digitalmars-d-learn mailing list