Proper Use of Assert and Enforce

Spacen Jasset spacenjasset at yahoo.co.uk
Wed Mar 14 13:15:16 PDT 2012


On 14/03/2012 09:59, Dmitry Olshansky wrote:
...
>
> To make it real simple:
> - assert on stuff you know has to be true regardless of circumstances
> and not dependent on any possible external factors.
> - enforce on stuff that must be true, but in general can fail like "file
> not found", etc. and/or depends on proper state of external factors you
> can't guarantee.
>
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.

...
 > A valuable trick, as enforce returns whatever passed to it or throws:
 >
 > backGround = enforce(SDL_SetVideoMode( xResolution, yResolution,
 > bitsPerPixel, SDL_HWSURFACE | SDL_DOUBLEBUF), "backGround is null!");
 >
Seems handy enough, if I understand it's purpose properly.
...



More information about the Digitalmars-d-learn mailing list