Testing and Debugging a Windows Forms Application - How to debug runtime errors
(Page 5 of 6 )
When a runtime error occurs, Visual Studio enters
break mode. In that mode, Visual Studio displays the Code Editor and highlights the statement that couldn’t be executed, displays the Debug toolbar, and displays an Exception Assistant dialog box like the one shown in figure 3-15. This is designed to help you find the cause of the exception (the
bug), and to
debug the application by preventing the exception from occurring again or by handling the exception. For example, you can often handle an exception by displaying an appropriate message to the user. This message may give the user one or more options for dealing with the exception.
Often, you can figure out what caused the problem just by knowing what statement couldn’t be executed, by reading the message displayed by the Exception Assistant, or by reading the troubleshooting tips displayed by the Exception Assistant. But sometimes, it helps to find out what the current values in some of the variables or properties in the program are. To do that, you can place the mouse pointer over a variable or property in the code so a data tip is displayed. This tip displays the current value of the variable or property.
For example, if the current value of the Text property of the txtSubtotal control is “ABC”, the data tip will clearly show that the Text property does not contain numeric data. However, the variable named subtotal requires numeric data. As a result, you know that the highlighted statement can’t be executed because the user didn’t enter numeric data in the Subtotal text box.
Once you find the cause of a bug, you can correct it. But first, you must exit from break mode. To do that, you can click the Stop Debugging button in the Debug toolbar. Then, you can correct the problem in the Code Editor and test the application again.
For now, don’t worry if you don’t know how to correct the problem in this example. Instead, you can assume that the user will enter valid data. In chapter 7, though, you’ll learn how to catch exceptions and validate all user entries for an application because that’s what a professional application has to do.
How a project looks in break mode

Figure 3-15. How to debug runtime errors
Description
- When an application encounters a runtime error, you need to fix the error. This is commonly referred to as debugging, and the error is commonly referred to as a bug.
- When an application encounters a runtime error, it enters break mode. In break mode, the Debug toolbar is displayed along with other windows that provide debugging features.
- When an application displays the Exception Assistant window, you can read a description of the error. This should give you an idea of what the error might be. You can also click on the links in the Troubleshooting Tips list to display more information in a Help window. You’ll learn more about debugging and the Exception Assistant window in chapter 11.
- To display a data tip for a property or variable, move the mouse pointer over it in the C# code. Then, its value is displayed in the data tip.
- To exit break mode and end the application, click the Stop Debugging button in the Debug toolbar or press Shift+F5. Then, you can attempt to fix the error, and you can run the application again when you’re done fixing the error.
Next: Perspective >>
More C# Articles
More By Murach Publishing
|
This article is excerpted from chapter three of the book Murach's C# 2005, written by Joel Murach (Murach, 2005; ISBN: 9781890774370). Check it out today at your favorite bookstore. Buy this book now.
|
|