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.

Finding and Fixing Run Time Errors

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.

Try It!

To inspect the value of a variable

  1. On the File menu, choose New Project.

  2. On the Templates pane, in the New Project dialog box, click Windows Application.

  3. In the Name box, type RunTimeErrors and then click OK.

    A new Windows Forms project opens.

  4. Double-click the form to open the Code Editor.

  5. In the Form_Load event handler, add the following code.

    Dim miles As Integer = 0
    Dim hours As Integer = 0
    Dim speed As Integer = 0
    miles = 55
    speed = miles / hours
    MsgBox(CStr(speed) & " miles per hour")
  6. 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.

  7. Move your mouse over the variable miles, and hold it there for a few seconds. You should see a ToolTip that reads "miles 55".

  8. 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.

  9. Add the following line of code above the line miles = 55.

    hours = 2
  10. 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.

  11. Press F5 to continue the program. A dialog box appears, displaying "28 miles per hour".

Next Steps

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.

Next Lesson:What? It Wasn't Supposed To Do That! Finding Logic Errors

See Also


Finding and Getting Rid of Compiler Errors

Know Your Bugs: Three Kinds of Programming Errors

Finding the Errors: Introduction to Visual Basic Debugging