
Beginning ASP.NET 2
.0.pdf
15
Dealing with Errors
This chapter covers another topic that you need to think about during the whole of site construction — that of how to deal with errors. In many ways this chapter could fit at the beginning of the book, because it’s highly likely you’ll get errors as you work through the book and what this chapter covers could be useful. But some of what’s discussed here uses code and depends upon other chapters, so we’ve left it until now.
It’s a fact that you will get errors when creating applications, and that’s okay. We all make mistakes, so this is nothing to be ashamed of or worried about. Some will be simply typing mistakes and some more complex, maybe due to lack of practice, but these go away with time (apart from my typing mistakes, but that’s another story!). So what this chapter looks at is a variety of topics covering all aspects of handling errors. In particular it examines the following:
How to write code so that it is error proof
What exceptions are and how they can be handled
How to centrally handle exceptions
How to use debugging and tracing to work out where errors are occurring
The first section looks at how to bulletproof code.
Defensive Coding
Defensive coding is all about anticipation; working out what could possibly go wrong and coding to prevent it. One of the precepts of defensive coding is that you should never assume anything, especially if user input is involved. Most users will be quite happy to use the site as intended, but hackers will search for ways to break into sites, so you have to do anything you can to minimize this risk.
Being hacked isn’t the only reason to code defensively. A coding bug or vulnerability may not be found by a user, but by yourself or a tester. Fixing this bug, then, involves resources — perhaps a project manager, a developer to fix the bug, a tester to retest the application, all of which take time and money. Also, any change to the code leads to potential bugs — there may not be any, but








