Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
142
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Chapter 5: Building Web Applications

this effect could have been achieved just as easily through CSS, but in future, when you’re working on projects that utilize more complex controls and properties, skins might be your only choice. As such, it’s important that you know how to use them.

Debugging and Error Handling

Your work with Dorknozzle for this chapter is over, but now that we’ve started to create a real-world application, it’s time to consider the real-world problems that might occur as we’re 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, it’s 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

Debugging with Visual Web Developer

The code above creates an array of ten elements, then uses a For loop to assign values to them. The problem is that it doesn’t stop at the tenth element: it also tries to assign a value to the eleventh element, which doesn’t exist.

If you load this page directly in Internet Explorer without debugging it, you’ll see a page that specifies the error, like the one shown in Figure 5.46.

Figure 5.46. The error message isn’t very helpful without debug mode

You can obtain more details by enabling debug mode and, if you scroll down, you’ll see instructions that explain how to do just that. The easiest way to enable debug mode it to use Visual Web Developer. You’ll remember from earlier in this chapter that the first time you execute a page by pressing F5 (Start Debugging), Visual Web Developer asks you if you want it to enable debug mode for you. If you ask it to, it’ll modify (or create) the Web.config file accordingly.

205

Chapter 5: Building Web Applications

Figure 5.47. Debugging a run-time error

Executing the page once again—this time, with debugging enabled—takes 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 you’ll see a little later, you can catch the error in your code using the Try-Catch-Finally construct. If the error isn’t caught and handled, as in this case, it’s finally caught by the ASP.NET runtime, which generates an error message.

In Figure 5.47, the debugger has paused execution at the moment the exception was raised. Let’s 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 it’s 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

Debugging with Visual Web Developer

Watch window; in response, it will display their values and types. Try typing a(5) (or a[5] if you’re 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 array’s 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, you’ll 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

Chapter 5: Building Web Applications

In more complex scenarios, if you enter the name of an object, the Watch window will let you explore its members as we just saw.

If you switch to the Locals window (Debug > Windows > Locals) shown in Figure 5.50, you can see the variables or objects that are visible from the line of code at which the execution was paused.

Figure 5.50. The Locals window

Another nice feature of Visual Web Developer is that when you hover your cursor over a variable, the editing window shows you at-a-glance information about that variable.

Sometimes, you’ll want to debug your application even if it doesn’t generate an exception. For example, you may find that your code isn’t generating the output you expected. In such cases, it makes sense to execute pieces of code line by line, and see in detail what happens at each step.

The most common way to get started with this kind of debugging is to set a breakpoint in the code. In Visual Web Developer, we do this by clicking on the gray bar on the left-hand side of the editing window. When we click there, a red bullet appears, and the line is highlighted with red to indicate that it’s a breakpoint, as Figure 5.51 illustrates.

Once the breakpoint is set, we execute the code. When the execution pointer reaches the line you selected, execution of the page will be paused and Visual Web Developer will open your page in debug mode. In debug mode, you can perform a number of tasks:

View the values of your variables or objects.

Step into any line of code by selecting Debug > Step Into. This executes the currently highlighted line, then pauses. If the selected line executes another local method, the execution pointer is moved to that method so that you can execute it line by line, too.

208

Debugging with Visual Web Developer

Figure 5.51. Setting a breakpoint

Step over any line of code by selecting Debug > Step Over. This makes the execution pointer move to the next line in the current method without stepping into any local methods that might be called by the current line.

Step out of any method by selecting Debug > Step Out. This causes the current method to complete and the execution to be paused on the next line of the method that called the current method.

Continue execution of the program normally by selecting Debug > Continue. Execution will stop again only if an exception is raised, or another breakpoint is met. If the execution is stopped as a result of an exception, choosing to continue the execution will allow the error to propagate to the ASP.NET runtime, which will cause the error message to display in the browser window.

Stop execution by selecting Debug > Stop Debugging.

Stop and restart the program by selecting Debug > Restart.

209