|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
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" Jan 02 2004
|