24.4 warning and error


24.4 #warning and #error

The directives #warning and #error are used to make the compiler display a compiler warning or error message. For warnings, the compiler displays the warning message and then carries on with the compilation. Compilation warnings do not cause compilation to terminate. On the other hand, with errors the compiler displays an error message and terminates the compilation process. Here is an example:

 1: using System;  2:  3: public class TestClass{  4:  5:   public static void Main(){  6:  #warning Main not yet completed  7:     Console.WriteLine("to be done");  8:     // insert code here after New Year holidays  9:     //... 10:   } 11: } 

Compilation warning:

 test.cs(6,11): warning CS1030: #warning: 'Main not yet completed' 

Output:

 c:\expt>test to be done 

If line 6 is replaced by:

 6: #error Main not yet completed 

a compilation error is produced, and no EXE file is created:

 c:\expt>csc test.cs test.cs(6,8): error CS1029: #error: 'Main not yet completed' 

#warning s and #error s are useful for absent minded developers dealing with lots of code simultaneously , and who need some helpful self-imposed reminders.

Note that you can place #warning s and #error s between #if and #endif 'blocks' so that a compilation warning or error appears only when certain symbols are defined.



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net