23
Visual Basic Debugging Tools Appendix D 06/12/22 1 Dr. Monther Aldwairi

Visual Basic Debugging Tools

Embed Size (px)

DESCRIPTION

Visual Basic Debugging Tools. Appendix D. Outline. Debugging strategies Error Types Compile, Run-Time and Logical errors VB Debugger Breakpoints, watches and stepping through Error Trapping Common Errors. Debugging. Errors in a program are called bugs - PowerPoint PPT Presentation

Citation preview

Page 1: Visual Basic Debugging Tools

Visual Basic Debugging Tools

Appendix D

04/20/23 1Dr. Monther Aldwairi

Page 2: Visual Basic Debugging Tools

Outline

• Debugging strategies• Error Types– Compile, Run-Time and Logical errors

• VB Debugger– Breakpoints, watches and stepping through

• Error Trapping• Common Errors

04/20/23 2Dr. Monther Aldwairi

Page 3: Visual Basic Debugging Tools

Debugging

• Errors in a program are called bugs• The process of finding and correcting them is

called debugging• Debugging aims at eliminating or reducing the

number of bugs in a program, thus making it behave as expected.

04/20/23 Dr. Monther Aldwairi 3

Page 4: Visual Basic Debugging Tools

Debugging strategies

• Tracing– Putting print statement in the code to verify flow and

variable values during execution– Remove print statements after the error is detected

• Logfile– Text file to which a debugging trace is written– Debuggers read the log file (GDB for example)

• Error Trapping and Handling– Used to trap fatal errors

04/20/23 4Dr. Monther Aldwairi

Page 5: Visual Basic Debugging Tools

Preferable Debug Method• Use a debugger!• Allows you to pause during execution in order

to view or alter variables1.Setting breakpoints (to suspend program

execution)2.Defining watch values (to see current value of

variables3.Stepping into a program (executing one

instruction at a time)

04/20/23 5Dr. Monther Aldwairi

Page 6: Visual Basic Debugging Tools

Error Types

• Compiler Errors– Syntactic, or compilation, errors occur when VB

commands are written improperly.

• Run-time errors – An application attempts to perform an action that

the system cannot execute

• Logical errors – Caused by faulty program logic.

04/20/23 6Dr. Monther Aldwairi

Page 7: Visual Basic Debugging Tools

Compiler Errors

• Visual Basic compiler errors occur when the compiler encounters problems in the code.

• A pop up windows is displayed

04/20/23 7Dr. Monther Aldwairi

Page 8: Visual Basic Debugging Tools

Run-time Errors

• Visual Basic run-time errors occur when an application attempts to perform an action that the system cannot execute.

04/20/23 8Dr. Monther Aldwairi

Page 9: Visual Basic Debugging Tools

Logical Errors• The program compiles and runs fine• The program does not give the expected output• General debugging strategy:

1. Place breakpoint near (preferably, slightly ahead) of suspected error source.

2. Execute program in normal fashion, until breakpoint is encountered.

3. Define one or more watch values and step through the program, one instruction at a time.

4. Follow watch values to identify location, and possibly, source, of error.

04/20/23 9Dr. Monther Aldwairi

Page 10: Visual Basic Debugging Tools

Program Modes

• Design– Place controls on the form, set properties and

write code

• Run (F5 or Start)• Break– Invoked automatically when run-time errors are

encountered– By clicking the break button or Ctrl-Break

04/20/23 10Dr. Monther Aldwairi

Page 11: Visual Basic Debugging Tools

Debugging Status Indicators

04/20/23 11Dr. Monther Aldwairi

Indicator Name

Symbol Description

Breakpoint Suspends program executionCurrent Statement

Represents the next line to be executed. You can drag the current statement indicator to another line to either skip or re-execute code

Page 12: Visual Basic Debugging Tools

Debug Menu

04/20/23 Dr. Monther Aldwairi 12

Page 13: Visual Basic Debugging Tools

Breakpoints

• Select statement, or click anywhere within statement, and set breakpoint:1. Select Toggle Breakpoint from the Debug menu, or2. Click on the Toggle Breakpoint button within the

Debug toolbar, or3. Press function key F9.

• A dark red point to the left of a statement indicates the existence of a breakpoint.

• The break in the program occurs just before the selected statement is executed.

04/20/23 13Dr. Monther Aldwairi

Page 14: Visual Basic Debugging Tools

Breakpoints

• To remove breakpoint use one of the three possibilities listed earlier to set a breakpoint.– To remove all break points select Clear All

Breakpoints from the Debug menu, or use Ctrl-Shift-F9

• Temporary breakpoint: click within a statement, select Run to Cursor from the Debug menu, or use Ctrl-F8.

04/20/23 Dr. Monther Aldwairi 14

Page 15: Visual Basic Debugging Tools

Defining watch values • Watch values are the current values of

variables or expressions• There are two types of watch values:

1. Ordinary watch values: remain active as you step through the program

2. Break watches: The cause a break when True or when the values changes

04/20/23 15Dr. Monther Aldwairi

Page 16: Visual Basic Debugging Tools

To define watch values

1. Select Add Watch, from the Debug menu, and enter information in the Add Watch dialog box.

2. Right-click on the Watches window, select Add Watch.

3. Highlight an expression in the Code Editor window, and select Add Watch, from the Debug menu.

4. Highlight an expression in the Code Editor window. Then, right-click and select Add Watch.

5. Shift+F9 for quick watches

04/20/23 16Dr. Monther Aldwairi

Page 17: Visual Basic Debugging Tools

Adding Watches

04/20/23 Dr. Monther Aldwairi 17

Page 18: Visual Basic Debugging Tools

Stepping through a program

• Three different types of stepping:1. Step Into results in line-by-line stepping of current

procedure and subordinated procedures.2. Step Over results in line-by-line stepping of current

procedure, bypassing subordinated procedures.3. Step Out results in execution of all remaining

statements within the current procedure, and then pauses at the first statement following the procedure access in the parent routine.

• Note procedures will be discussed later in CH4

04/20/23 18Dr. Monther Aldwairi

Page 19: Visual Basic Debugging Tools

Stepping through a program

• To carry out line-by-line stepping do one of the following:1. Select Step Into (or Step Over, Step Out) from

the Debug menu2. Press F8 to Step Into, Shift+F8 to Step Over,

Ctrl+Shift+F8 to Step Out3. Click on the Step Into button (or Step Over or

Step Out button) on the Debug toolbar

04/20/23 Dr. Monther Aldwairi 19

Page 20: Visual Basic Debugging Tools

Error Trapping

• We will teach you later in Chapter 8 how to trap and handle fatal errors

04/20/23 20Dr. Monther Aldwairi

Page 21: Visual Basic Debugging Tools

Common Error Types

# Error Type # Error Type

6 Overflow 58 File already exists

7 Out of memory 61 Disk full

9 Subscript out of range 62 Input past end of file

11 Division by zero 71 Disk not ready

13 Type mismatch 380 Invalid property value

52 Bad file name or number 382 'Item' property cannot be set at run time

52 File not found 423 Property or method not found

55 File already open 450 Wrong number of arguments

04/20/23 21Dr. Monther Aldwairi

Page 22: Visual Basic Debugging Tools

Compile Errors Examples

04/20/23 22Dr. Monther Aldwairi

Page 23: Visual Basic Debugging Tools

Run Time Errors

04/20/23 23Dr. Monther Aldwairi