Error Handling
It should be of some comfort that you will catch a lot of bugs and errors in your code before it is ever shipped or deployed to a client. On the other hand, it’s also true that users, innocent or otherwise, will find ways to break your pages regardless of the efforts you make to validate their input and (imperceptibly) control their progression through your site. And it’s up to you to add some level of error handling into the site so that the site does not crash or, if possible, return some cryptic .NET error message that your users don’t understand and you don’t get to see.
Fortunately, VS2008 will simply refuse to compile your code at all if it contains any syntax errors—violations of the rules of C# and Visual Basic. Those aside, however, the subtler bugs and those likely to make it through to the live server are errors in logic.
To demonstrate, open Default.aspx in the C19_Debugging website and change this line:
for (int i = 0; i < bookList.GetLength(0); i++)
to this:
for (int i = 0; i < bookList.GetLength(0) + 2; i++)
When this code runs, it will try to add more items to the book array than the array has space allotted for, thus causing a runtime error. This is not a subtle bug, but it serves to demonstrate how the system reacts to runtime errors. If you run the page, the error is detected straight away and a generic error page (a yellow screen of death) appears with full details of the error, as shown in Figure 19-24.
This error page is actually fairly useful to the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access