Debugging Tools in dot net

Programming errors are inevitable. Even very experienced programmers occasionally introduce errors while writing code. These errors are referred to as bugs. Debugging is the process of locating and fixing these errors. In this lesson, you will learn about the kinds of errors you are likely to encounter and the tools available to isolate and correct these errors.

Types of Errors

There are three basic types of errors you might encounter when writing code. Syntax errors represent code that cannot be understood by the compiler. Run-time errors are errors that attempt an operation that is impossible to carry out. Additionally, logical errors are errors that compile and execute correctly but return an unexpected result. In this section, we will examine each type in turn.

Syntax Errors

A syntax error occurs when the compiler cannot compile the code it is given. For example, a syntax error occurs when keywords are typed incorrectly, punctuation is omitted, or an incorrect construct is parsed.

First, a period is missing between MessageBox and Show, creating an unrecognizable command for the compiler. Second, the closing construct for the method is missing (End Sub in Microsoft Visual Basic .NET, } in Microsoft Visual C#). Both errors will create an uninterpretable condition for the compiler.

Microsoft Visual Studio .NET allows you to easily identify syntax errors. Whenever you build your project, syntax errors are automatically detected and underlined in the code.

You can double-click any error in the Task List window, and the cursor will immediately highlight that error in the code window. Frequently, this is sufficient information to correct the error. However, if further information is required, you can obtain help for the highlighted item by pressing F1.

Visual Basic .NET performs automatic syntax checking when code is typed. Syntax errors are underlined and added to the Task List window when they are typed. Building the project is not necessary. Visual C# also performs automatic syntax checking when code is typed, but the level of error detection is not as sophisticated.

To identify and correct syntax errors

Build the project. Any syntax errors will be detected and added to the Task List window. If you are working in Visual Basic .NET, syntax errors will automatically be added to the Task List window without building the project.

Double-click an error in the Task List window. The error will be highlighted in the code editor.

Correct the error. If necessary, you can obtain help by pressing F1.

Run-Time Errors

Run-time errors occur when the application attempts to perform an operation that is not allowed. This includes operations that are impossible to carry out, such as division by zero, and operations that are not allowed, as in the case of security exceptions. When a run-time error occurs, an exception describing the error is thrown. Exceptions are special classes that are used to communicate error states between different parts of the application. You can write code to handle exceptions so that they do not halt execution of your application.

Logical errors occur when the application compiles and executes correctly, but does not produce the expected results. These can be the most difficult type of errors to track down because there might not be indications as to the source of the error. Logical errors can result from something as innocuous as a misplaced decimal point.

The method in the example takes a value for an hourly wage and calculates a yearly salary by multiplying it by the number of weeks in the year and hours in a week. The error occurs because the value is multiplied by 400 instead of 40 hours per week. This method will still compile and execute correctly, but inappropriate results will be reported. You can detect logical errors by feeding test data into your application and analyzing the results.

Break Mode

Break mode allows you to halt program execution and execute code one line at a time. While in Break mode, you can examine the values of application variables and properties. Visual Studio .NET enters Break mode under any of the following circumstances:

You choose Step Into, Step Over, or Step Out from the Debug menu or toolbar.

Execution reaches a line that contains an enabled breakpoint.
Execution reaches a Stop statement.

An unhandled exception is thrown.

Once in Break mode, you can use the debugging tools to examine your code. Table 5.1 summarizes the features found on the Debug menu.

Using Step Into

You can use Step Into to examine execution of your program line-by-line. When you choose Step Into to step through your code, the application executes the current line, and then returns to Break mode. If the current line is a method call, the debugger advances to the first line of that method. After the line has been executed, you can examine the state of your application using the various debugging windows.

To use Step Into

Select Step Into from the Debug menu or from the Debug toolbar, or press F11.

Using Step Over

You can use Step Over in much the same manner in which you use Step Into. Step Over behaves identically to Step Into except that when a function call is encountered, Step Over executes the entire function call and advances to the next line. You should use Step Over when you want to stay at the same level in your code and do not need to analyze any nested method calls.

To use Step Over

Select Step Over from the Debug menu or from the Debug toolbar, or press F10.

Using Step Out

Step Out allows you to execute the remainder of code in the current procedure. If the current procedure was called from another method, execution advances to the statement following the line that called that procedure.

To use Step Out

Select Step Out from the Debug menu or from the Debug toolbar, or press Shift+F11.

Using Run To Cursor

The Run To Cursor option allows you to choose a line of code and execute all of the code in the application up to that line. You can use this option to skip over blocks of code that do not need to be executed line-by-line. For example, you might use Run To Cursor to step over a large loop.

To use Run To Cursor

In the code editor, place the cursor on the line you want to run to. This line must be an executable line of code. Right-click and choose Run To Cursor.

Using Set Next Statement

You can use Set Next Statement to skip over sections of code. Set Next Statement allows you to set the next statement within the current method to be executed, skipping over any intermediate code. Unlike Run To Cursor, the code that is skipped is not executed. You can use Set Next Statement to skip sections of code that contain known bugs. You can also use Set Next Statement to choose a line of code that has already been executed.

To use Set Next Statement

In the code editor, place the cursor on the line you want to designate as the next executed line. This line must be an executable line of code within the current procedure. Right-click and choose Set Next Statement. You can then use Step Into to execute the line.

Setting Breakpoints

You can designate lines of code or conditions that will always cause the application to break in the debugger. These are called breakpoints. You can use break-points to set lines of code or conditions that will halt program execution. There are four types of breakpoints:

Function breakpoints. Cause the application to enter Break mode when a specified location within a function is reached.

File breakpoints. Cause the application to enter Break mode when a specified location within a file is reached.

Address breakpoints. Cause the application to enter Break mode when a specified memory address is accessed.

Data breakpoints. Cause the application to enter Break mode when the value of a variable changes. This option is not available when programming in Visual Basic .NET and Visual C#.

To set a function breakpoint

Function breakpoints are the most commonly used breakpoints. You can set a function breakpoint in one of three different ways:

By clicking the gray bar to the left of the code window at the desired line. This will insert a breakpoint at the specified line.

By right-clicking the desired line of code and choosing Insert Breakpoint from the pop-up window.

The Breakpoints window lists all of the breakpoints in your project, where the breakpoint is located, and any conditions attached to the breakpoint. You can choose additional columns of information about your breakpoints from the Columns drop-down menu. You can disable a breakpoint by clearing the check box next to it. Also, the button in the top-left corner of the window allows you to create a new breakpoint, delete a breakpoint, or clear or disable all breakpoints.

Clearing and Disabling Breakpoints

When a breakpoint is no longer needed, you can remove it. Or, if you want to retain a breakpoint but do not need it at that moment, you can disable it. As mentioned earlier, you can use the Breakpoints window to manage your breakpoints. You can also remove or disable a breakpoint in the code editor.

To remove a breakpoint

Click the breakpoint in the gray bar to the left of the code editor. This will remove the breakpoint.

To disable a breakpoint

Right-click the breakpoint in the code editor and choose Disable Breakpoint. This will disable the breakpoint.

Using the Debugging Windows

Visual Studio provides several tools you can use to monitor program execution. Some, such as the Output window and the Command window, are available in Design mode. Others, such as the Locals window, are visible only while debugging. This section discusses some of the windows frequently used in debugging.

The Output Window

The Output window displays all of the command-line output from the application as it is compiled and executed. This includes notifications from the application that assemblies have loaded as well as output from Debug and Trace statements. When debugging your application, you will primarily use the Output window to receive Debug notifications.

By choosing New Breakpoint from the Debug menu or by right-clicking in the code editor and choosing New Breakpoint from the pop-up menu. You can then add a new breakpoint by setting the appropriate parameters in the New Breakpoint window.

When setting a new breakpoint with the New Breakpoint window, you can attach conditions that determine whether or not execution halts when the breakpoint is reached.

Pressing the Condition button displays the Breakpoint Condition window, which allows you to designate an expression and causes the breakpoint to be active only if the expression is true or if the expression changes. Pressing the Hit Count button opens the Breakpoint Hit Count window, which allows you to set the breakpoint to be active only when it has been hit a predetermined number of times.

The Locals, Autos, and Watch Windows

The Locals, Autos, and Watch windows are windows that allow you to monitor the status of program variables while the application is being debugged. Additionally, these windows allow you to edit the values of variables while debugging. This can be useful when testing how procedures respond to different input.

The Locals Window

When the program is running in Debug mode, the Locals window allows you to monitor the values of all the variables in the current procedure. The Locals window contains three columns of information: a Name column that displays the name of the variable, a Value column that displays the variable's value, and a Type column that displays the variable's type. Complex types such as classes or structures are displayed in a tree view that can be expanded to reveal the values of their members.

As program execution shifts from method to method, the variables displayed within the Locals window will shift so that only the local variables are displayed. You can alter the value of a variable in the Locals window by selecting its value under the Value column and typing a new value. Note that only strings and numeric values can be changed in this manner. You cannot set a class or structure variable to refer to another instance of that class or structure, although you can change the values of variables within the class or structure. When a value has been changed, it appears in red in the Value column.

To display the Locals window

While debugging your application, choose Debug, then Windows, and select Locals.

The Autos Window

The Autos window can be thought of as an abbreviated form of the Locals window. The Autos window displays the same data columns as the Locals window, but only displays the variables in the current line and the previous line (Visual C#) or the variables in the current line and three lines on either side of the current line (Visual Basic .NET). You can alter the value of a variable in the Autos window just as you would in the Locals window.

To display the Autos window

While debugging your application, choose Debug, then Windows, and select Autos.

The Watch Window

With the Watch window, you can designate a variable to track. Variables added to the Watch window remain in the window even when they are no longer in scope. Like the Locals and Autos windows, the Watch window displays the variable's Name, Value, and Type, and you can alter the running values of noncomplex variables by typing a new value into the Value column for the appropriate variable. Visual Studio .NET provides four separate Watch windows so you can designate multiple sets of watch variables.

You can quickly evaluate a variable by using the QuickWatch dialog box. The QuickWatch dialog box shows you the Name, Value, and Type of a single variable, and gives you the option of adding the variable to the Watch window. You can edit the value of the variable in the QuickWatch dialog box by typing a new value in the Value column.

To display the Watch window

While debugging your application, choose Debug, then Windows, and select either Watch1, Watch2, Watch3, or Watch4.

To display the QuickWatch dialog box

In the code editor, right-click a variable and choose QuickWatch from the pop-up menu.

To add a variable to the Watch window

You can add a variable to the Watch window in one of the following ways:

Select an empty row in the Watch window and type the variable name.

In the code editor, right-click on a variable and choose Add Watch from the pop-up menu.

Using the mouse, highlight and drag a variable from the code editor, the Autos window, or the Locals window.

In the QuickWatch dialog box, press the Add Watch button.

The Command Window—Immediate Mode

When debugging, the Immediate mode of the Command window can be used to execute procedures, evaluate expressions, or change variable values.

The Immediate window can evaluate any valid statements or method calls but cannot accept data declarations. When in Break mode, you can enter a statement or method call in the Command window as you would in the code editor. When you press Enter, Visual Studio switches to run time and executes the statement. When execution of the statement or method call is complete, the application returns to Break mode.

When the application is in Break mode, you can print variable values directly in the Command window. You can use the question mark (?) to print the value of an expression or variable in the Command window. The following example multiplies the current values of X and Y and prints them to the Command window .

X*Y

Note that the syntax is the same for both Visual Basic .NET and Visual C#. In the case of C#, no semicolon is required to terminate the line. You can use the same syntax to print the values of any property that is currently in scope. For example:

TextBox1.Text

prints the current value of the Text property of TextBox1.

You can use the assignment operator to assign a new value to an existing variable. The following example sets the value of X to 5:

X = 5
Note that the variable must already be declared.

To display the Command window

From the View menu, choose Other Windows, then Command Window. You can also display the Command window in Immediate mode by selecting Debug, then Windows, and selecting Immediate.

Other Windows

There are several additional windows that you can use while debugging to obtain information on your program's execution or to manage Break mode. Table 5.3 summarizes the other windows that are available from the Debug menu.

related post

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS


No comments:

Post a Comment