Chapter 13 Error-handling and Debugging (Main Page) 13.1 A

CHAPTER 13
ERROR HANDLING AND DEBUGGING
Chapter 13 Error-handling and Debugging
13.1
13.2
13.3
13.4
13.5
13.6
13.7
13.8
13.9
13.10
13.11
13.12
13.13
13.14
13.15
13.16
13.17
13.18
13.19
13.20
13.21
13.22
13.23
13.24
13.25
13.26
13.27
13.28
13.29
13.30
13.31
13.32
13.33
(Main Page)
A simple error-handling example with divide by zero.
Dialog displayed when division-by-zero error is not handled.
Demonstrating nested O n Error statements.
Common Err object properties and methods.
Program that uses the Err object.
Resum eflow of control.
Demonstrating Resum e.
A typical call sequence.
Call stack error-handling program.
Rethrowing an error.
Design mode, run mode, and break mode title bars.
Run menu items and related tool bar buttons.
Error dialog.
Immediat eWindow.
Demonstrating simple error handling in a procedure.
Debug menu and debug tool bar.
Debug menu and debug tool bar commands.
Current executable line (in break mode).
Executing a statement (in break mode).
Stepping into procedure initializeFlags.
Stepping into initializeFlags.
Demonstrating Run To Curso .r
A breakpoint.
Execution suspending at a breakpoint.
Local swindow.
Local swindow showing form properties.
Changing a local variable’s value.
Add Watc h dialog.
Watch window.
Quick Watc hwindow.
Local swindow.
Call Stac window.
k
Green triangle identifying a procedure’s calling point.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
1
CHAPTER 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ERROR HANDLING AND DEBUGGING
' Fig. 13.1
' Divide-by-zero error-handling program
Option Explicit
' General declaration
Private Sub lblResult_Click()
Dim numerator As Double
Dim denominator As Double
numerator = txtInput(0).Text
denominator = txtInput(1).Text
On Error GoTo divideByZeroHandler
' Set trap
lblResult.Caption = "Result is " & numerator / denominator
Exit Sub
' Prevents error-handler from being executed
' if an error is not raised
divideByZeroHandler:
' Label for error handler
lblResult.Caption = "Attempted divide by zero!"
End Sub
Private Sub txtInput_GotFocus(Index As Integer)
lblResult.Caption = "Click Here to Perform Division"
txtInput(Index).Text = ""
' Clear TextBox with focus
End Sub
Initial GUI at execution.
GUI after the user has entered two values and clicked the Label.
Fig. 13.1
A simple error-handling example with divide by zero (part 1 of 2).
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
2
CHAPTER 13
ERROR HANDLING AND DEBUGGING
GUI after the user has entered two values (the denominator input is 0) and
clicked the Label.
Fig. 13.1
A simple error-handling example with divide by zero (part 2 of 2).
Fig. 13.2
Dialog displayed when a division-by-zero error is not handled.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
' Fig. 13.3
' Divide-by-zero error-handling program
Option Explicit
' General declaration
Private Sub lblResult_Click()
Dim numerator As Double
Dim denominator As Double
On Error GoTo inputHandler
numerator = txtInput(0).Text
denominator = txtInput(1).Text
' Nested On Error statement
On Error GoTo divideByZeroHandler
lblResult.Caption = "Result is " & _
numerator / denominator
Exit Sub
' Prevents errorhandler from being executed
' if an error is not raised
divideByZeroHandler:
' Label for error handler
lblResult.Caption = "Attempted divide by zero!"
Exit Sub
' Prevent next handler from executing
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
3
CHAPTER 13
25
26
27
28
29
30
31
32
ERROR HANDLING AND DEBUGGING
inputHandler:
lblResult.Caption = "Attempted non-numeric input!"
End Sub
Private Sub txtInput_GotFocus(Index As Integer)
lblResult.Caption = "Click Here to Perform Division"
txtInput(Index).Text = ""
' Clear TextBox with focus
End Sub
GUI after the user has entered a invalid
input and clicked the Label.
Fig. 13.3
Demonstrating nested On
Property/method
Error statements.
Data type
Description
Description
String
A String describing the error.
Number
Long
Internal error number determined by Visual Basic
or a component with which the program interacts
(such as a control).
Source
String
File name where the error was raised or the source
of the error is the error was outside the application.
Properties
Methods
Clear
Clears an error—numeric properties are set to 0
and String properties are set to empty
Strings.
Raise
Raises an error.
Fig. 13.4
Common Err object properties and methods .
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
4
CHAPTER 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
' Fig. 13.5
' Using the Err object
Option Explicit
' General declaration
Private Sub cmdPrint_Click()
Dim num As Integer
Call Cls
Font.Size = 12
' Clear form
' Set form Font size to 12 pt.
On Error GoTo errorHandler
' Set trap
' Randomly manufacture an error
Select Case Int(Rnd() * 3)
Case 0
num = 888888888
' Overflow error
Case 1
num = 88 / 0
' Divide-by-zero error
Case 2
num = "an Error" ' Type mismatch
End Select
Exit Sub
Fig. 13.5
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
ERROR HANDLING AND DEBUGGING
' Exit procedure
Program that uses the Err object (part 1 of 2).
errorHandler:
Print "Source: " & Err.Source
Print "Error: ";
Select Case Err.Number
Case 6
' Overflow
ForeColor = vbYellow
Case 11
' Divide by zero
ForeColor = vbWhite
Case 13
' Type mismatch
ForeColor = vbBlue
Case Else
Print "Unexpected error!!!"
End Select
Print Err.Description
ForeColor = vbBlack
End Sub
' Print description
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
5
CHAPTER 13
ERROR HANDLING AND DEBUGGING
Initial GUI at execution.
GUI after user clicks Print. One of
three possible Descriptions is
printed.
Fig. 13.5
Program that uses the Err object (part 2 of 2).
1
On Error Goto label
1 Error trap is enabled. Any
error raised results in label’s error
handler being called.
Procedure/Function code
someLabel:
.
.
2 If an error is raised, control
transfers to the error handler.
2
Exit Sub/Exit Function
3 If Resume is executed, control returns to the line where the
error was raised. The line is executed again in its entirety.
label:
error-handling code
.
.
.
3
Resume statement
5
Fig. 13.6
4 If Resume Next is executed,
control returns to the next executable line after the line that
raised the error.
Resume Next statement
Resume someLabel
4
5 If Resume someLabel is executed, control continues at the
label someLabel (which can
appear anywhere within the
procedure or function).
Resume flow of control.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
6
CHAPTER 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
ERROR HANDLING AND DEBUGGING
' Fig. 13.7
' Resume example
Option Explicit
' General declaration
Private Sub cmdGo_Click()
Dim x As Integer
Dim s As String
resumeLabel:
s = "Visual Basic How to Program!"
Print s
On Error GoTo handler
' InputBox can raise OverFlow and Type
' mismatch errors
x = InputBox("Enter an integer:", s)
Print "Value of x is " & x
Exit Sub
handler:
If optResume.Value Then
Print "Resume: ";
Resume
' Repeat line that raised error
ElseIf optResumeNext.Value Then
Print "Resume Next: ";
Resume Next ' Resume at next line after error-raising line
Else
' optResumeLabel
Print "Label: ";
Resume resumeLabel ' Resume at label resumeLabel
End If
End Sub
Private Sub optResume_Click()
Call Cls
End Sub
Private Sub optResumeLabel_Click()
Call Cls
End Sub
Private Sub optResumeNext_Click()
Call Cls
End Sub
Fig. 13.7
Demonstrating Resume (part 1 of 3).
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
7
CHAPTER 13
ERROR HANDLING AND DEBUGGING
Initial GUI at execution.
Pressing Go results in a
dialog being displayed.
Close box
prompt
Dialog displayed
by function
InputBox
GUI after the user has pressed
Go and entered valid data in
the input dialog. When an
error is raised, the input dialog is repeatedly displayed
until valid input occurs.
GUI after user has entered a
data value of 88 (which does
not raise an error) and
attempted invalid input
(which raises an error). When
an error is raised and Resume
Next is selected, x is not
assigned a value from the
input dialog, therefore x has
the default initialization of 0.
Fig. 13.7
Demonstrating Resume (part 2 of 3).
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
8
CHAPTER 13
ERROR HANDLING AND DEBUGGING
GUI after user has entered a
data value of 44 (which does
not raise an error) and
attempted invalid input
(which raises an error). When
an error is raised and Resume
at resumeLabel is selected,
the String Visual Basic How
to Program! is printed and
the input dialog repeatedly
displays until valid data is
input.
Fig. 13.7
Demonstrating Resume (part 3 of 3).
one
.
.
Call two
.
.
two
.
.
Call three
.
.
three
.
.
x = four
.
.
four
.
.
four = y
.
.
Fig. 13.8
1
2
3
4
5
6
7
8
9
10
' Fig. 13.9
' Resume and the call stack
Option Explicit
' General declaration
Dim mFlags(4) As Boolean
' General declaration
Private Sub Form_Load()
Call initializeFlags
optSelect(4).Value = True
End Sub
Fig. 13.9
11
12
13
14
15
16
17
18
19
A typical call sequence.
Call stack error-handling program (part 1 of 4).
Private Sub initializeFlags()
Dim x As Integer
For x = LBound(mFlags) To UBound(mFlags)
mFlags(x) = False
imgImage(x).Picture = LoadPicture("d:\images\" & _
"ch13\resume1.gif")
Next x
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
9
CHAPTER 13
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
End Sub
Private Sub setFlag(n As Integer)
Call initializeFlags
mFlags(n) = True
End Sub
Private Sub cmdBegin_Click()
If mFlags(0) Then
On Error GoTo handler
Else
On Error GoTo 0 ' Disable error handler
End If
Call one
Exit Sub
' Call procedure one
' handler's Resume Next executes here
handler:
lblLocation.Caption = "Error handled in cmdBegin_Click"
imgImage(0).Picture = LoadPicture(d:\images\" & _
"ch13\resume3.gif")
Resume Next ' Exit Sub statement
End Sub
Private Sub one()
If mFlags(1) Then
On Error GoTo handlerOne
Else
On Error GoTo 0 ' Disable error handler
End If
Call two
Exit Sub
' Call procedure two
' handlerOne's Resume Next executes here
handlerOne:
lblLocation.Caption = "Error handled in one"
imgImage(1).Picture = LoadPicture(d:\images\" & _
"ch13\resume3.gif")
Resume Next
' Exit Sub statement
End Sub
Fig. 13.9
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
ERROR H ANDLING AND DEBUGGING
Call stack error-handling program (part 2 of 4).
Private Sub two()
If mFlags(2) Then
On Error GoTo handlerTwo
Else
On Error GoTo 0 ' Disable error handler
End If
Call three
Exit Sub
' Call procedure three
' handlerTwo's Resume Next executes here
handlerTwo:
lblLocation.Caption = "Error handled in two"
imgImage(2).Picture = LoadPicture(d:\images\" & _
"ch13\resume3.gif")
Resume Next ' Exit Sub statement
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
10
CHAPTER 13
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
End Sub
Private Sub three()
Dim x As Integer
If mFlags(3) Then
On Error GoTo handlerThree
Else
On Error GoTo 0 ' Disable error handler
End If
x = four()
Exit Sub
' Call function four
' handlerThree's Resume Next executes here
handlerThree:
lblLocation.Caption = "Error handled in three"
imgImage(3).Picture = LoadPicture(d:\images\" & _
"ch13\resume3.gif")
Resume Next ' Exit Sub statement
End Sub
Private Function four() As Integer
If mFlags(4) Then
On Error GoTo handlerFour
Else
On Error GoTo 0 ' Disable error handler
End If
' Draw image representing function raising error
imgImage(4).Picture = LoadPicture(d:\images\" & _
"ch13\resume2.gif")
Err.Raise Number:=6
' Cause Overflow Error
Print "This is NEVER printed!!!"
Fig. 13.9
114
115
116
117
118
119
120
121
122
123
124
125
126
ERROR H ANDLING AND DEBUGGING
Call stack error-handling program (part 3 of 4).
handlerFour:
lblLocation.Caption = "Error handled in four"
imgImage(4).Picture = LoadPicture(d:\images\" & _
"ch13\resume3.gif")
End Function
Private Sub cmdGo_LostFocus()
lblLocation.Caption = ""
End Sub
' Clear Label
Private Sub optSelect_Click(Index As Integer)
Call setFlag(Index)
End Sub
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
11
CHAPTER 13
ERROR H ANDLING AND DEBUGGING
Initial GUI at execution.
GUI after user selects two
and presses Go.
Fig. 13.9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Call stack error-handling program (part 4 of 4).
' Fig. 13.10
' Demonstrating rethrowing an error
Option Explicit
Private Sub cmdRethrow_Click()
On Error GoTo handler
Call RethrowTheError
Exit Sub
handler:
Print "cmdRethrow_Click's error handler executed!"
Resume
' Resume at line that raised error
End Sub
Private Sub RethrowTheError()
Dim x As Integer
On Error GoTo localHandler
x = InputBox("Enter a number") ' Error can be raised here
Print "You entered " & x
Exit Sub
localHandler:
If Err.Number = 13 Then
' Type Mismatch error
Print "Rethrowing error..."
Call Err.Raise(13)
' Rethrow error
Else
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
12
CHAPTER 13
30
31
32
33
ERROR H ANDLING AND DEBUGGING
Print "Error: " & Err.Description
End If
End Sub
Initial GUI at execution.
Fig. 13.10
Rethrowing an error (part 1 of 2).
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
13
CHAPTER 13
ERROR H ANDLING AND DEBUGGING
Initial GUI at execution.
GUI after user has entered valid data
in the input dialog.
GUI after user attempted invalid
input. Error was raised, resulting in
input dialog being repeatedly displayed until valid input occurs.
Fig. 13.10
Rethrowing an error (part 2 of 2).
Fig. 13.11
Design mode, run mode and break mode title bars.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
14
CHAPTER 13
ERROR H ANDLING AND DEBUGGING
Tool bar buttons
Run menu items
Break
Fig. 13.12
Run menu items and related tool bar buttons.
Fig. 13.13
Error dialog.
Typed by user
Typed by user
Printed by Visual Basic
Fig. 13.14
Immediate Window.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
15
CHAPTER 13
ERROR H ANDLING AND DEBUGGING
Sub generalProcedure ()
' Local declarations
...
On Error Goto handlerLabel ' Set trap
' Procedure Code inside trap
...
Exit Sub
handlerLabel: ' error-handler
' Code that handles error
...
' Specify where to resume control
Resume
End Sub
Fig. 13.15
Demonstrating simple error handling in a procedure.
Debug tool bar
Debug menu
Fig. 13.16 Debug menu and debug tool bar.
Command
Description
Step Into
Executes the current line. If the current line is a procedure or function call, the procedure or function code is entered.
Step Over
Executes the current line. If the current line is a procedure or function call, the procedure or function is executed without stepping
into its code.
Step Out
Execute the remaining procedure or function code and continue
stepping through the procedure or function caller’s code.
Run To Cursor
Executes all lines of code up to the line containing the cursor.
Add Watch...
Displays the Add Watch dialog. (See Section 13.10.3.)
Fig. 13.17
Debug menu and debug tool bar commands.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
16
CHAPTER 13
ERROR H ANDLING AND DEBUGGING
Command
Description
Edit Watch...
Displays the Edit Watch dialog. (See Section 13.10.3.)
Quick Watch...
Displays the Quick Watch dialog. (See Section 13.10.3.)
Toggle Breakpoint
Adds or removes a breakpoint.
Clear All Breakpoints
Removes all breakpoints.
Set Next Statement
Allows the programmer to specify which statement within the procedure or function is executed next.
Show Next Statement
Transfers the cursor to the next line to be executed.
Fig. 13.17
Debug menu and debug tool bar commands.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
17
CHAPTER 13
ERROR H ANDLING AND DEBUGGING
Fig. 13.18
Current executable line (in break mode).
Fig. 13.19
Executing a statement (in break mode).
Fig. 13.20
Stepping into procedure initializeFlags.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
18
CHAPTER 13
ERROR H ANDLING AND DEBUGGING
initializeFlags.
Fig. 13.21
Stepping into
Fig. 13.22
Demonstrating Run To Cursor.
Margin
indicator bar
Fig. 13.23
A breakpoint.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
19
CHAPTER 13
ERROR H ANDLING AND DEBUGGING
Fig. 13.24
Execution suspending at a breakpoint.
Fig. 13.25
Locals window.
Fig. 13.26
Locals window showing form properties.
Fig. 13.27
Changing a local variable’s value.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
20
CHAPTER 13
ERROR H ANDLING AND DEBUGGING
Fig. 13.28
Add Watch dialog.
Fig. 13.29
Watch window.
Fig. 13.30
Quick Watch window.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
21
CHAPTER 13
ERROR H ANDLING AND DEBUGGING
Call Stack button
Fig. 13.31
Locals window.
Fig. 13.32
Call Stack window.
Fig. 13.33
Green triangle identifying a procedure’s calling point.
© Copyright 1999 Prentice Hall. All Rights Reserved.
For use only by instructors in courses for which Visual Basic 6 How to Program is the required textbook.
22