Proper Use of Assert and Enforce

Chris Pons cmpons at gmail.com
Tue Mar 13 22:44:19 PDT 2012


I'm new, and trying to incorporate assert and enforce into my 
program properly.

My question revolves around, the fact that assert is only 
evaluated when using the debug switch. I read that assert throws 
a more serious exception than enforce does, is this correct?

I'm trying to use enforce in conjunction with several functions 
that initialize major components of the framework i'm using.

However, i'm concerned with the fact that my program might 
continue running, while I personally would like for it to crash, 
if the expressions i'm trying to check fail.

Here is what i'm working on:

	void InitSDL()
	{
		enforce( SDL_Init( SDL_Init_Everything ) > 0, "SDL_Init 
Failed!");

		SDL_WN_SetCaption("Test", null);

		backGround = SDL_SetVideoMode( xResolution, yResolution, 
bitsPerPixel, SDL_HWSURFACE | SDL_DOUBLEBUF);

		enforce( backGround != null, "backGround is null!");

		enforce( TTF_Init() != -1, "TTF_Init failed!" );
	}

Is it proper to use in this manner? I understand that I shouldn't 
put anything important in an assert statement, but is this ok?


More information about the Digitalmars-d-learn mailing list