D - 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
{
...
}
Dec 30 2003
"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"contractI threw one and it said: Throw statements cannot be in contractsI can't think of a good reason why not.
Jan 02 2004








"Walter" <walter digitalmars.com>