this effect could have been achieved just as easily through CSS, but in future,
when youre working on projects that utilize more complex controls and properties,
skins might be your only choice. As such, its important that you know how to
use them.
Debugging and Error Handling
Your work with Dorknozzle for this chapter is over, but now that weve started
to create a real-world application, its time to consider the real-world problems
that might occur as were developing that application. A constant truth in the
life of any programmer is that programming mistakes do happen, and they happen
no matter how experienced the programmer is. For this reason, its beneficial to
know what you can do when you encounter an error, and to learn how ASP.NET
and Visual Web Developer can help you analyze and debug your code.
Debugging with Visual Web Developer
Take a look at this code:
Visual Basic File: ErrorTest.aspx.vb (excerpt)
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Dim a(10) As Integer
Dim i As Integer
For i = 1 To 11
a(i) = i
Next
End Sub
C# File: ErrorTest.aspx.cs (excerpt)
protected void Page_Load(object sender, EventArgs e)
{
int[] a = new int[10];
int i;
for (i = 0; i < 11; i++)
{
a[i] = i;
}
}
204
Chapter 5: Building Web Applications
The code above creates an array of ten elements, then uses a For loop to assign
values to them. The problem is that it doesnt stop at the tenth element: it also
tries to assign a value to the eleventh element, which doesnt exist.
If you load this page directly in Internet Explorer without debugging it, youll
see a page that specifies the error, like the one shown in Figure 5.46.
Figure 5.46. The error message isnt very helpful without debug
mode
You can obtain more details by enabling debug mode and, if you scroll down,
youll see instructions that explain how to do just that. The easiest way to enable
debug mode it to use Visual Web Developer. Youll remember from earlier in
this chapter that the first time you execute a page by pressing F5 (Start Debug-
ging), Visual Web Developer asks you if you want it to enable debug mode for
you. If you ask it to, itll modify (or create) the Web.config file accordingly.
205
Debugging with Visual Web Developer
Figure 5.47. Debugging a run-time error
Executing the page once againthis time, with debugging enabledtakes you
straight to the error in Visual Web Developer, as Figure 5.47 illustrates.
This interface tells you that the code has thrown an exception of type
IndexOutOfRangeException. In .NET, exceptions are the standard means by
which errors are generated and propagated. An exception is a .NET class (in this
case, the IndexOutOfRangeException class) that contains the details of an error.
As youll see a little later, you can catch the error in your code using the
Try-Catch-Finally construct. If the error isnt caught and handled, as in this
case, its finally caught by the ASP.NET runtime, which generates an error mes-
sage.
In Figure 5.47, the debugger has paused execution at the moment the exception
was raised. Lets see what your options are at this moment. One very useful
window is the Watch window, which appears by default when your application
is being debugged. If its not displayed, you can open it by accessing Debug >
Windows > Watch. You can type the names of the objects in your code into the
206
Chapter 5: Building Web Applications
Watch window; in response, it will display their values and types. Try typing
a(5) (or a[5] if youre using C#) in the Watch window; you should see a display
like the one in Figure 5.48.
Figure 5.48. Inspecting values using the Watch window
You could even type just a, then explore its members via the display shown in
Figure 5.49.
Figure 5.49. The Watch window showing the contents of an array
Arrays and VB
This example reveals an interesting aspect of this array. The Watch window
reports that the arrays length is 11, yet we defined it as a(10). In all .NET
languages, arrays are zero-based, which means that the first element of an
array is a(0), the second is a(1), and so on. So an array called a that had
ten elements would have as its first element a(0), and a(9) as its last.
However, VB offers extra assistance for developers who are experienced with
pre-.NET versions of the language (which had one-based arrays in which the
first element would have been a(1), and the last would have been a(10)):
it adds an element for you. In other words, if you declare an array of ten
elements in VB, youll get an array of 11 elements.
C# has always had zero-based arrays, so an array defined as a[10] will have
ten elements.
207
Debugging with Visual Web Developer

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.