Assert and the optional Message

Jonathan M Davis jmdavisProg at gmx.com
Fri Mar 9 11:06:46 PST 2012


On Friday, March 09, 2012 19:23:20 Timon Gehr wrote:
> On 03/09/2012 05:56 PM, Jonathan M Davis wrote:
> > The current implementation may not skip them, but if so, that might
> > actually be a bug. Errors are not intended to be recoverable, so you
> > can't rely on them hitting finally blocks, scope statements, or
> > destructors. They may very well do so at present. While it's not
> > guaranteed that they will, I'm not sure that it's guaranteed that they
> > _won't_. So, it may or may not be a bug if they do.
> > 
> > It was never intended that AssertError or any other Error be particularly
> > catchable, but it's also true that D is a systems programming language, so
> > it'll let you do stuff which is unsafe, so if you know what you're doing,
> > then there are circumstances where you can get away with (unit testing is
> > one example of where catching AssertErrors might make sense). But you
> > have to know what you're doing, since it's _not_ safe.
> > 
> > - Jonathan M Davis
> 
> AssertErrors must be recoverable because of the way contracts work.

In what way? Yes, they're _catchable_, but everything that was on the unwound 
portion of the stack is now in an undefined state. So, recovering from the 
AssertError and continuing execution doesn't work. So, a handler can catch the 
AssertError and do something before letting it kill the program, but portions 
of the program are definitely in an undefined state when an AssertError is 
caught. The closer the catch point is to the throw point, the less of an issue 
that is, but unless you can guarantee that no destructors, scope statements, 
or finally blocks were between the throw point and the catch point, then some 
portion of the program is in an undefined state, and continuing program 
execution is a bad idea. It _is_ possible to catch an AssertError, but you 
have to be very careful about what you do after that.

I'm not aware of anything in contracts which would involve catching an 
AssertError and then continuing execution. When a contract fails, it's going 
to kill the program just like any other assertion failure does. So, I'm not 
quite sure what you're referring to.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list