Error Types: Syntax Errors
Error Types: Syntax Errors
pieces of specimen code provided, the VB debugging tools should be used to trace the source of problems
when programs do not behave as expected.
Error Types
In writing VB software, three types of errors can occur:
Syntax errors
These are grammatical errors in the formulation of statements and are picked up by the interpreter while you
are typing in the code (providing the syntax checking option - under environment options - is set to yes). In
the example below the syntax of the assignment statement is incorrect - the property of Label1 is missing
(the statement should be Label1.Caption = current_day). Visual Basic has signalled this error at design
time by placing the cursor at the point of omission and displaying a pop up error message.
Run-time errors
These are errors that cannot be detected until the program is running. The syntax of the statements is correct,
but on execution they cause a situation to arise that results in a crash or an undefined value. Error handlers
can be used to trap such errors and deal with them (these are beyond the scope of this course).
Examples of run-time errors are attempted division by zero or trying to access a non-existent object as in the
example below (where Label2 has not been created).
Logic errors
These are errors that cause the program to behave incorrectly. They generally arise through failure on the
part of the programmer to arrive at a correct algorithm for the task. Typical problems might be incorrect
ordering of statements, failure to initialise or re-initialise a variable, assignment to an incorrect variable, use
of ‘<’ instead of ‘<=’, use of ‘and’ instead of ‘or’, or omission of a crucial step in the processing. Logic
errors may lurk in a program even when it appears to work - they may only surface under certain conditions.
This is why careful testing is so important.
Program errors can be very tricky to isolate. Sometimes they occur at the end of a long series of calculations
or after many iterations of a loop. Programs execute very quickly and there is generally no opportunity to
verify exactly what has been executed unless you build in diagnostic statements (such as print statements
added to produce a trail, or to display intermediate calculations).
Clear all
A fast way to remove all set breakpoints.
Breakpoints
Break mode halts execution of the software and gives a snapshot of its state at a particular moment. Variable
and property settings are preserved and may be inspected. In break mode you can:
1. Watch pane
Located directly beneath the Debug window title bar, this pane is visible only if you have selected a
watch expression to be monitored during code execution. The displayed information includes the
context of the watch expression, the expression itself and its value at the time of the transition to
break mode. If the context of the expression is not in scope when going to break mode, the current
value is not displayed.
2. Immediate pane
Located below the title bar, or below the Watch pane if it is displayed, the Immediate pane is where
you enter code to execute it immediately. While working in the Immediate pane:
The Debug window can do different things depending on what mode Visual Basic is in:
Break Mode
When execution of a program has been temporarily halted and VB is in Break mode the Debug window can
be used to execute single line commands entered by the user. For example, the user can click on the Debug
window, type ‘Print Time$’, and hit . The result is the current system time is displayed in the Debug
window. Almost any line of code can be executed from the Debug window. You can also use the Debug
window to monitor the value of expressions you have selected as watch expressions.
At run time, although you can’t enter statements into the Debug window, you can use it to display internal
values without printing them to the actual forms. The Debug window is an object which has a single Print
method. For example, the following line will send the name of the font used in ‘Label1’ to the debug
window at run time:
Debug.Print Label1.FontName
Debug.Print intCurrentDay
Debug.Print "Reached here in the code"
Note: The VB program which is still running may obscure the Debug window, thus it is advisable to move it
so that the contents of the debug window can be seen.
Debugging Checklist
Debugging is a process by which you find and resolve errors in your code. To debug code in Visual Basic,
consider the ideas suggested below. These techniques can also be applied in different sequences.
1. Print the code, if you find it easier to read code on paper instead of online.
2. Run the application to find trouble spots:
o From the Run menu, choose ‘Start’ to begin running the application.
o Run until an error stops execution, or halt execution manually when you suspect an error by
choosing ‘Break’ from the Run menu.
o Resolve all compile errors and run-time errors.
o From the Run menu, choose ‘Continue’ to continue running the application.
3. Use debugging tools and techniques to isolate bugs and to monitor code flow:
o Set breakpoints to halt execution at certain points to look for problems such as incorrect
variable types, mix-ups in variable names, flaws in logical comparisons, endless loops,
garbled output, problems with arrays, and so on. You can also set breakpoints to monitor
code flow or examine information in the current state.
o Add a watch expression at design time or while in break mode to allow you to monitor the
value of a particular expression as your code execution progresses.
o Single step or procedure step through your code to trace through the application as it’s
running.
o Use the Debug window to test individual lines of code or procedures, or to change values.
o Enter break mode and choose ‘Calls’ from the Debug window to see where your code is
currently executing and trace the path showing how it got there.
4. Try out bug fixes and then make edits:
o Test individual lines of new or debugged code in the Debug window.
o Search and replace code for all occurrences of an error, checking other procedures, forms, or
modules with related code.
o From the Run menu, choose ‘Restart’ to reset application variables and properties and
restart the application from the beginning.