Proper Use of Assert and Enforce

Jonathan M Davis jmdavisProg at gmx.com
Wed Mar 14 18:06:32 PDT 2012


On Wednesday, March 14, 2012 20:15:16 Spacen Jasset wrote:
> Is enforce then a way of generating exceptions in an easier way rather
> than using some sort of "if (failure) throw" syntax? In other words, I
> assume it's a mechanism to help you use exceptions, and not some new
> semantic.

It is purely a way to make throwing an exception use a syntax similar to 
assert and save a line of code.

if(!condition)
    throw new Exception(msg);

becomes

enforce(condition, msg);

or

enforce(condition, new Exception(msg));

It arguably adds very little, but some people really like it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list