December 31, 2003 throw in contract, and assert | ||||
---|---|---|---|---|
| ||||
Why can't we throw exceptions in contracts? In my function's "out" contract I threw one and it said: Throw statements cannot be in contracts But I can call a function that throws one, and even assert throws one. I wanted to use an exception so the error has a meaningful text reason, not just assert at this particular line. Isn't this the reason for exceptions and the toString() member function? But even if assert did take a message, sometimes I might still want to throw an exception so I don't need to catch asserts, just like I might not want a regular assert used for array bounds checks, etc. There is a workaround, but it defeats the purpose of the contracts: int asdf(int i) { debug(0) { if(!i) throw new GeneralAsdfException("Meaningful asdf expected."); } ... } instead of: int asdf(int i) in { if(!i) throw new GeneralAsdfException("Meaningful asdf expected."); } body { ... } |
January 03, 2004 Re: throw in contract, and assert | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | "Vathix" <vathix@dprogramming.com> wrote in message news:bstdke$283s$1@digitaldaemon.com... > Why can't we throw exceptions in contracts? In my function's "out" contract > I threw one and it said: > Throw statements cannot be in contracts I can't think of a good reason why not. |
Copyright © 1999-2021 by the D Language Foundation