#508 – Using the #error and #warning Directives

The #error and #warning directives allow you to output errors and warnings at compile time.  You can give either directive some text that will be output by the compiler and look like a standard compile-time error or warning.

In the example below, we’ve include a compile-time warning, reminding us that we want to later change some code.

#warning TODO: Add a parameter to this Bark method
            d.Bark();

At compile time, we’ll see this warning in the Error List in Visual Studio.

You might use the #error directive to force an error to occur based on some combination of conditional compilation symbols.  When this directive is encountered, the compiler will generate an error.

#if DEBUG && BUILDFORRELEASE
#error You fool--Release builds must not be built with DEBUG flag!!
#endif

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: