#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