Debugging and Error Handling
So far in this book, you have covered all the basics of simple programming in C#. Before you move on to look at object-oriented programming in the next section of the book, it's time to look at debugging and error handling in C# code.
Errors in code are something that will always be with you. No matter how good a programmer is, there will always be problems that slip through, and part of being a good programmer is realizing that this is the case and being prepared to deal with it. Of course, these may be minor problems that don't affect the execution of an application, perhaps a spelling mistake on a button or the like. They may also be glaring errors that cause applications to fail completely (usually known as fatal errors). Fatal errors include simple errors in code that will prevent compilation (syntax errors), but may be more involved and only occur at runtime. Alternatively, errors may be subtler. Perhaps your application will fail to add a record to a database if a requested field is missing or adds a record with the wrong data in other restricted circumstances. Errors such as these, where application logic is in some way flawed, are known as semantic errors (also known as logic errors).
Often, the first that you might hear about the more subtle errors will be when a user of your application complains that something isn't working properly. This leaves you with the task of tracing through your code to try to find out what is happening and how ...