Debugging & error handling
Back to Main Page
Even the most careful programmers will get errors when coding - usually simple typing mistakes.
Fortunately, VB has a number of ways of detecting all types of error - we cal these errors '
bugs' in the program. Debugging means removing errors so that the program runs correctly.


The 3 error types are:

Syntax errors - mistakes in spelling or misuse of the VB language.
VB will highlight misuses of the language by putting a  yellow bar over the incorrect code
e.g. declaring a variable named Firstnumber, but mistyping it as Fisrtnubmer later in the code
e.g. typing lop instead of loop

Run-time errors - cause the program to stop working
e.g. trying to store a very large number as an Integer or causing a loop that never ends

Logic errors - a mistake in the logic of the code, often due to poor planning
e.g. assigning the incorrect value to a variable, such as pi = 2.142, instead of 3.142


VBs debugging facility allows you to '
step through' your code, looking at it one line at a time.
It also allows you to keep a '
watch' on your variables, to see how they change as the program runs.


Task 1 - type in the code

Type in the code below - the purpose of the program is to display the powers of 2 up to the power 10.

i.e. 2 to the power 1 is 2  (2 x 1 = 2)
      2 to the power 2 is 4  (2 x 2 = 4)
      2 to the power 3 is 8  (2 x 2 x 2 = 8)

Run your program to confirm that the answer doubles every time
Task 2 - Stepping through code and setting watches on a variable

We will now use the
watch and step facilities to examine how VBs debugging procedures work.

From the top menu bar select
Debug -> Add Watch.
Set a watch on the variable named Number by typing
Number in to the Expression box. This will display what value the variable Number has at each stage of the For...Next loop.
Do the same for the
Counter variable - this will enable you to track what stage the loop is at as the program runs.


The
watch window opens and the message 'Out of context' appears for both variables - don't worry, this is because the program isn't running yet!

Open the
debug toolbar to allow 'one-click' steps - this is quicker than using the drop-down menus.

To do this, click on
View menu -> Toolbars -> Debug. The debug toolbar is shown below:
Task 3 - stepping through the program

Click on the '
Step Into' symbol - 5th from the left and VB will highlight each line of code in yellow as it steps.

If your watch window disappears when you do this, click on the 'watch' icon
to get it back.

Initially, Number and Counter have the value 0 - VB sets all new variables to 0.
Click the
Step Into icon again and the next line of code is highlighted.

Question:

At which line of code will the value of Number change -  what will it change to?


Keep stepping, and monitor the values of Number and Counter as the loop progresses.
When the program reaches the last line click the 'Stop' icon -
this will end the watch process.

In future, you can use these facilities to track your code and correct any errors.


End of Tasks
Hosted by www.Geocities.ws

1