DEBUGGING IN VB
Basic Knowledge
y Writing Codes y Using Controls y Fundamentals of Programming y Objects and its instances y Custom Controls
CONTENT
y What is debugging? y Types of Error y Debug Menu y Using Break Mode
What is Debugging?
y Errors in a program is known as a bug. y Finding the bug and removing them is known as
debugging.
TYPES OF ERROR
y Syntax Error y Run Time Error y Logical Error
SYNTAX ERROR
y A syntax error occurs in code when a statement is constructed
incorrectly.
y Examples of syntax errors include an incorrectly typed keyword,
omission of required punctuation, or an incorrect construct (such as an If keyword on a line without a conditional operator).
SYNTAX ERROR
y The following example shows a syntax error in an IfThen statement:
Dim intCount As Integer If intCount = 0 MsgBox "Bad number" If is a valid word in the Visual Basic language, but without a then, it doesn't meet the syntax requirements.
SYNTAX ERROR
y Visual Basic includes an Auto Syntax Check option that can detect syntax errors
as you type in a statement. With this option enabled, Visual Basic interprets your code as you enter it.
y
When an expected part of the syntax is not found, Visual Basic highlights the incorrect statement and displays a message box explaining the error.
running and are detected by Visual Basic when the statement attempts an operation that is impossible to carry out
The variable sngHours contains zero, making the division an invalid operation, even though the statement itself is syntactically correct.
error. y You can include code in your application to trap and handle run-time errors when they occur.
LOGICAL ERROR
y Logic errors occur when your code does not perform
the way that you intended. y An application can run without performing any invalid operations and still produce incorrect results.
LOGICAL ERROR
y The way to verify that your application does not have
logic errors is to run test data through the program and analyze the results. y Logic errors, can occur when the user is interacting with the application.
To CLEAR ERRORS?
y There are several tools you can use to debug your
application. y They are accessed from the debug menu or toolbar, or by a shortcut key. y The most useful tools are:
Step Into Step Over Step Out Set Next Statement Show
Next Statement
design mode : Design mode lets you customize forms and write code. The program has not been compiled yet and is not running. run mode : Run mode lets you check the execution of your program's code, but no statements can be changed in this mode.
break mode: Break mode halts the application, and gives you an opportunity to check the condition of your program at that moment. However, the program has not terminated. It is only suspended, allowing you to look around at the current "frozen" state of the application. Break mode is critical when debugging an application.
execute your code one statement at a time. y You can also view the value of one or more variables or control properties. y To do this you must first enter into break mode to get to your code window.
circumstances:
You choose one of the Step options from the debug menu or toolbar. Execution reaches a line that contains a breakpoint. You set a breakpoint by selecting the line where the break should occur and clicking the Toggle Breakpoint button. Execution reaches a Stop statement. A break expression defined in the Add Watch dialog box changes or becomes true. An untrapped run-time error occurs.
y Once your program is placed in break mode, you can use the
Action
Shortcut key
Description
Break
CTRL+BREAK
Stops execution of a program temporarily. Click the Continue button to resume running the program.
F9
Creates or removes a breakpoint. A breakpoint is a place in the code where Visual Basic automatically halts execution and enters break mode. Runs the next executable line of code, stopping at the next executable line of code that follows. If the next executable line of code calls another procedure, Step Into stops at the beginning of that procedure.
F8
Action
Shortcut key
Description
Step Over
SHIFT+F8
Runs the next executable line of code, stopping at the next executable line of code that follows. If the next executable line of code calls another procedure, the procedure runs completely before stopping at the next executable line of code in the first procedure.
Step Out
CTRL+SHIFT+F Executes the remainder of the current procedure and breaks at the next executable 8 line of code in the calling procedure.
Annexure - A
y Auto Correct Options