It Doesn't Work! Finding and Eliminating Run-Time Errors
In this lesson, you will learn how to debug your program and fix run-time errors.
As you learned earlier, run-time errors occur when your program attempts an operation that is impossible to complete. When a run-time error occurs, the program stops and an error message is displayed—you need to debug the error and fix it before the program can continue.
Most run-time errors occur because you made a mistake in your code, for example, forgetting to assign a value to a variable before you use it. When you run your program and the mistake is discovered, the program will stop and the Exception Assistant dialog box is displayed in the Code Editor window. When this occurs, the program is in break mode, the mode in which debugging is done.
The Exception Assistant dialog box contains a description of the error, as well as troubleshooting tips that suggest the cause. You can click the troubleshooting tips to display Help topics for more details.
You need to fix the error before the program can continue—in order to do so, you need to inspect your code to find the cause of the error. For example, if you suspect that an error occurred because a variable contains the wrong value, while you are still in break mode, you can use IntelliSense to see the value of the variable. When you hold the mouse over the variable in the Code Editor, a ToolTip displays the value of the variable. If the value is not what you expected, look at the preceding code to see where the value was set, then fix that code and continue.
To inspect the value of a variable
On the File menu, choose New Project.
On the Templates pane, in the New Project dialog box, click Windows Application.
In the Name box, type RunTimeErrors and then click OK.
A new Windows Forms project opens.
Double-click the form to open the Code Editor.
In the Form_Load event handler, add the following code.
Press F5 to run the program. An Exception Assistant dialog box is shown with the message "OverflowException was unhandled".
A dotted line from the dialog box to your code file shows you which line of code caused the error.
Notice that the first troubleshooting tip in the Exception Assistant suggests that you should make sure you are not dividing by zero.
Move your mouse over the variable miles, and hold it there for a few seconds. You should see a ToolTip that reads "miles 55".
Now move your mouse over the variable hours—the ToolTip should read "hours 0".
Because you cannot divide by zero and the value of hours is zero, you found the cause of the error—not updating the value of hours.
Add the following line of code above the line miles = 55.
Click the yellow arrow in the left-hand margin of the code and drag it up to the line hours = 2.
This allows the program to continue from that line, rather than the line that contains the error. This is required to execute the new line of code you just added, in order for your error fix to be recognized.
Press F5 to continue the program. A dialog box appears, displaying "28 miles per hour".
In this lesson, you learned how to find and fix run-time errors. In the next lesson, you will learn about the third type of programming error—logic errors. You can explore more advanced debugging techniques in Closer Look: What If... Testing Code in the Immediate Window, and then proceed to the logic errors lesson.