Error Handling

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

ERROR-HANDLING AND DEBUGGING

Lecture 5

1
ERROR TYPES
 Errors can be grouped into three categories:
 Syntax errors/compile time
 Run-time errors
 Logic errors
 Syntax errors occur when you mistype a command or leave out an expected
phrase or argument. Visual Basic detects these errors as they occur and even
provides help in correcting them. You cannot run a Visual Basic program
until all syntax errors have been corrected.
 Run-time errors are usually beyond your program's control. Examples
include: when a variable takes on an unexpected value (divide by zero),
when a drive door is left open, or when a file is not found. Visual Basic
allows you to trap such errors and make attempts to correct them.
 Logic errors are the most difficult to find. With logic errors, the program
will usually run, but will produce incorrect or unexpected results. The
Visual Basic debugger is an aid in detecting logic errors.
2

CONT’ED
 Some ways to minimize errors:
 Design your application carefully. More design time means less
debugging time.
 Use comments where applicable to help you remember what you
were trying to do.
 Use consistent and meaningful naming conventions for your
variables, objects, and procedures.

3
RUN-TIME ERROR TRAPPING AND
HANDLING
 Run-time errors are trappable. That is, Visual Basic recognizes an
error has occurred and enables you to trap it and take corrective
action. If an error occurs and is not trapped, your program will
usually end in a rather unceremonious manner.
 Error trapping is enabled with the On Error statement:

 On Error GoTo errlabel

 Yes, this uses the dreaded GoTo statement! Any time a run-time
error occurs following this line, program control is transferred to
the line labeled errlabel. Recall a labeled line is simply a line with
the label followed by a colon (:).

4
CONT’ED
 The best way to explain how to use error trapping is to look at an outline of an
example procedure with error trapping.
Sub SubExample()
.
. [Declare variables, ...]
.
On Error GoTo HandleErrors
.
. [Procedure code]
.
Exit Sub
HandleErrors:
.
. [Error handling code]
5
.
End Sub
CONT’ED
 Once an error has been trapped and some action taken, control
must be returned to your application. That control is returned via
the Resume statement. There are three options:
 Resume Lets you retry the operation that caused the error.
That is, control is returned to the line where the error occurred.
This could be dangerous in that, if the error has not been corrected
(via code or by the user), an infinite loop between the error
handler and the procedure code may result.
 Resume Next Program control is returned to the line immediately
following the line where the error occurred.
 Resume label Program control is returned to the line labeled
label.

6
EXAMPLE-SIMPLE ERROR TRAPPING

7
DEBUGGING VISUAL BASIC PROGRAMS
 The interface between your application and the debugging tools is via
three different debug windows: the Immediate Window, the Locals
Window, and the Watch Window. These windows can be accessed
from the View menu (the Immediate Window can be accessed by
pressing Ctrl+G). Or, they can be selected from the Debug Toolbar
(accessed using the Toolbars option under the View menu):

 All debugging using the debug windows is done when your application is in
break mode. You can enter break mode by setting breakpoints, pressing
Ctrl+Break, or the program will go into break mode if it encounters an
untrapped error or a Stop statement.

8
CONT’ED
 Once in break mode, the debug windows and other tools
can be used to:
 Determine values of variables
 Set breakpoints
 Set watch variables and expressions
 Manually control the application
 Determine which procedures have been called
 Change the values of variables and properties

 When you run your program, Visual Basic will stop when it
reaches lines with breakpoints and allow you to use the
immediate window to check variables and expressions. To
continue program operation after a breakpoint, press <F5>,
click the Run button on the toolbar, or choose Start from
the Run menu. 9
USING THE DEBUGGING TOOLS
 There are several debugging tools available for use in Visual
Basic. Access to these tools is provided with both menu options
and buttons on the Debug toolbar. These tools include
breakpoints, watch points, calls, step into, step over, and step
out.
1. Breakpoints:
 A breakpoint is a line in the code where you want to stop
(temporarily) the execution of the program, that is force the
program into break mode. To set a breakpoint, put the cursor in the
line of code you want to break on. Then, press <F9> or click the
Breakpoint button on the toolbar or select Toggle Breakpoint
from the Debug menu. The line will be highlighted.

10
2. VIEWING VARIABLES IN THE LOCALS WINDOW:
The locals window shows the value of any variables within
the scope of the current procedure.
 As execution switches from procedure to procedure, the
contents of this window changes to reflect only the
variables applicable to the current procedure.

11
WATCH EXPRESSIONS
 The Add Watch option on the Debug menu allows you to
establish watch expressions for your application. Watch
expressions can be variable values or logical expressions
you want to view or test. Values of watch expressions are
displayed in the watch window.
 In break mode, you can use the Quick Watch button on the
toolbar to add watch expressions you need. Simply put the
cursor on the variable or expression you want to add to the
watch list and click the Quick Watch button.
 Watch expressions can be edited using the Edit Watch
option on the Debug menu.

12
CALL STACK
 Selecting the Call Stack button from the toolbar (or pressing
Ctrl+L or selecting Call Stack from the View menu) will display
all active procedures, that is those that have not been exited.
 Call Stack helps you unravel situations with nested procedure
calls to give you some idea of where you are in the application.
Call Stack Example:
 Set a breakpoint on the Fcn = Cint() line in the general function
procedure. Run the application. It will break at this line.
 Press the Call Stack button. It will indicate you are currently in
the Fcn procedure which was called from the Command1_Click
procedure. Clear the breakpoint.

13
SINGLE STEPPING (STEP INTO):
 While at a breakpoint, you may execute your program one line at a
time by pressing <F8>, choosing the Step Into option in the
Debug menu, or by clicking the Step Into button on the toolbar.
 This process is single stepping. It allows you to watch how
variables change (in the locals window) or how your form changes,
one step at a time.
 You may step through several lines at a time by using Run To
Cursor option. With this option, click on a line below your current
point of execution. Then press Ctrl+<F8> (or choose Run To
Cursor in the Debug menu). the program will run through every
line up to the cursor location, then stop.

14
PROCEDURE STEPPING (STEP OVER):
 While single stepping your program, if you come to a procedure
call you know functions properly, you can perform procedure
stepping. This simply executes the entire procedure at once,
rather than one step at a time.
 To move through a procedure in this manner, press Shift+<F8>,
choose Step Over from the Debug menu, or press the Step Over
button on the toolbar.

15
FUNCTION EXIT (STEP OUT):
 While stepping through your program, if you wish to complete
the execution of a function you are in, without stepping through
it line-by-line, choose the Step Out option.
 The function will be completed and you will be returned to the
procedure accessing that function.
 To perform this step out, press Ctrl+Shift+<F8>, choose Step
Out from the Debug menu, or press the Step Out button on the
toolbar.

16

You might also like