Chapter 7: Debugging and Error Handling


Overview

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 you can change your code so that it does what it was intended to do.

In situations like this, you will find that the debugging capabilities of VS are a fantastic help. In the first part of this chapter, you look at some of the techniques available and apply them to some common problems.

In addition to this, you will also look at the error-handling techniques available in C#. These enable you to take precautions in cases where errors are likely, and write code that is resilient enough to cope with errors that might otherwise be fatal. These techniques are part of the C# language rather than a debugging feature of VS, but VS does provide some tools to help you here too.

In this chapter you look at:

  • The debugging methods available in Visual Studio

  • The error-handling techniques available in C#




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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