Debugging an Application

 <  Day Day Up  >  

After you place any of the necessary breakpoints for your code, you can start debugging by pressing the F5 key or selecting Start Debug from the Build menu. Note that you don't have to build your project before debugging because the IDE performs that step before launching the debugger. The following sections introduce the various commands and tool windows that are at your disposal as you debug an application.

Stepping with the Keyboard

Stepping through code line-by-line to investigate the order of execution and the effect each statement makes to variables is the procedure used most when debugging. You use three methods to step through lines of source when debugging after a breakpoint is reached. Although you are more than welcome to use the stepping commands using the Debug menu within the main menu or using the Toolbar buttons , I recommend learning the three keyboard shortcuts to speed up the process without adding miles of cursor movement on your mouse.

The first method is stepping into statements, which you accomplish by pressing the F11 key. If the next statement to execute is a method call within your project, the debugger enters into the first executable line in that method and allows you to begin stepping through that method. If at any point you want to step out of the called method and return to the calling statement, press the key sequence Shift+F11. Execution commences at the next executing line in the calling method. If you want to simply execute a method without stepping into it, press the F10 key. The method runs, but the debugger does not enter into it, and the debugger stops at the next executing statement in your current location.

Using Watch Windows

Watch windows allow you to view the current state of a variable. If the variable is a primitive type, the watch window displays its value. If the variable is a class or struct, the watch window creates a treeview of that variable, allowing you to view the individual members. Additionally, if a member variable is also a complex data type, each of its members is contained within a child tree node as well. As you step through your code, the watch window updates any variables it is currently watching to reflect their new values.

There are a total of four different watch windows and a QuickWatch window. To display a watch window, click on Debug, Windows, Watch and select one of the four windows. To add a variable to the watch window, highlight the variable name within the source code window and drag and drop it into the watch window. The variable is always displayed regardless of your current position in the debugger. If the variable cannot be determined based on the current context, the watch window reflects that fact with an error message instead of the variable's value.

The watch windows are not only designed to contain variables. You can also create a watch for a certain statement as long as the statement does not contain method calls. The statement can contain any number of variables with associated properties in addition to indexer values for collection types.

The QuickWatch window is a modal dialog window that allows you to quickly inspect the value of a variable or statement. To access the QuickWatch window, highlight the item to view, right-click on it, and select the QuickWatch menu item. Using this window is similar to the other watch windows but to continue debugging, you must close the window.

Viewing the Call Stack

As your application is executing, a stack called the Call Stack follows the current position of the executing context with regards to the current method being executed. Whenever a method is entered, its address is placed on top of the stack with the calling method directly underneath it. When a breakpoint is hit, you can view the current call stack by selecting Debug, Windows, Call Stack. The top line of the window represents the method you are currently debugging, and each subsequent line represents the method that was currently executing before the current method was called. You can double-click any method within the call stack, and if the method is contained within your project code, the corresponding line in the source code for that method is displayed. Even though you might see method names that are contained within the .NET Framework assemblies, you cannot view the corresponding source code.

Debugging with Exceptions

If you are debugging an application and an exception is thrown but not handled, the debugger displays a message asking you to break at that location, continue, or abort the debugging process. If you click on the Break button, you are placed at the location the exception occurred.

You can also customize the debugger to break whenever any exceptions are thrown regardless of whether they are caught by accessing the Exceptions window through the Debug menu item. All the possible .NET exceptions and WIN32 exceptions appear in a treeview control. You can select a group of exceptions such as Common Language Runtime Exceptions and change the corresponding behavior the debugger should take when the exception is thrown. You can also target a specific exception by expanding the group the exception is within and selecting the exception name. You can then change the properties using the radio button settings for that exception only.

Additional Debugging Tool Windows

Two additional tool windows available when debugging behave similarly to the watch windows, but their functionality is automated by the debugger. The Locals window displays all the variables defined within the current executing context. In other words, the Locals window displays all the local parameters accessible from the current method being executed.

The Autos window is similar to that of the Locals window, but it limits the visibility of the current locals. Rather than display all the local variables within the currently executing method, it only displays those that are being currently accessed within the current statement and those that were accessed in the previous statement. You can immediately see a before and after snapshot of a variable after it is accessed by a statement.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net