0% found this document useful (0 votes)
42 views

Error Types: Syntax Errors

The document discusses debugging tools in Visual Basic, including breakpoints, stepping through code, and using the Debug window. It describes three types of errors that can occur: syntax errors, run-time errors, and logic errors. Syntax errors are detected at design time, while run-time and logic errors require debugging tools to detect.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Error Types: Syntax Errors

The document discusses debugging tools in Visual Basic, including breakpoints, stepping through code, and using the Debug window. It describes three types of errors that can occur: syntax errors, run-time errors, and logic errors. Syntax errors are detected at design time, while run-time and logic errors require debugging tools to detect.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Just as VB Help should be used frequently to check on the syntax of commands or to explore the many

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 (Compile errors)


 Run-time errors
 Logic errors

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.

Using debugging tools


Debugging tools are designed to help with:

 Stopping the execution of a program at specific points


 Detecting run-time and logic errors
 Understanding the behaviour of error-free code

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

The VB environment includes tools to make debugging easier:

Debug Menu Purpose


Step Into Executes one line of code and then pauses; clicking again on this
option will execute the next line. When the interpretter comes across a
subroutine call it will step into and execute each line within the
subroutine separately.
Step Over Similar to ‘Step Into’ except when encountering subroutine calls.
When coming across subroutine calls the interpretter will step over all
the code within the subroutine in one go.
Step Out All lines of code in the current subroutine are executed and the
execution point is paused once again on the line after the subroutine
call.
Run to When selected the current project will be started and the program run
Cursor until it reaches the line where the cursor currently is (like a temporary
breakpoint).
Add Watch
Edit Watch
Quick
Watch
Toggle Adds and removes breakpoints (see screen shot below) where the
Breakpoint cursor currently is. The program will run normally until a line
containing a breakpoint at which time it will pause execution.
Breakpoints can also be toggled by clicking on the grey margin in the
code window.

Clear all
A fast way to remove all set breakpoints.
Breakpoints

Using Break Mode


At design time you can change the interface or code but the effect on the executing program cannot be seen
until run time. At run time you can observe the behaviour of the program, but not change it.

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:

 Observe the state of the interface.


 Determine which procedures are active.
 Control which statement will be executed next.
 Watch the values of variables and properties.
 Single step through following code.
 Make changes to code

Using the Debug window


Sometimes debugging requires analysis of what is happening to data. A problem may have been traced to
the value of a variable or property but you need to know where the incorrect assignment occurred. The
Debug window lets you display the value of variables either while a program is running or after it has been
halted. The window consists of two panes:

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:

o You can execute only one line of code at a time.


o You can type or paste a line of code and press to run it.
o You cannot save code, but you can copy and paste it into the Code window.
o In Break mode, a statement in the Debug window is executed in the context displayed in the
Debug window title bar. For example, if you type Print variable name your output is the value
of a local variable. This is the same as if the Print method had actually occurred in the
procedure you were executing when the program halted.

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.

Run time Mode

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

Variables or constants can also be printed in the same way:

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.

You might also like