Download - Debugging
1
Error Handling
In VB.net
22
Error Types
In VB.net errors fall into one of two main categories: Syntax Errors Runtime Errors Logic Errors
Each type of error has a number of methods which can help the programmer identify and fix the problem. This process is called Debugging.
33
Syntax ErrorsSyntax (or compiler) errors occur when you write code that does not follow the rules of the language. When the compiler recognises a syntax error, it
marks the error with a blue wavy line.
44
Debugging Syntax Errors Moving the cursor over the marked code
causes an error message to appear.
Clicking on the error list, takes you directly to the offending line
55
Runtime ErrorsRuntime errors are the result of code that despite having the correct syntax, cannot be executed. if not handled, will generate an exception, that
will cause the program to halt.
This type of error is often caused by an unexpected variable value e.g. missing or invisible wrong data type
Anticipate code that may result in runtime errors and provide ways to avert crashes.
66
Debugging Runtime ErrorsUsing the debugger, accessed via the Debug menu or toolbar. Setting a breakpoint- pause the program at a particular
line of code or when a variable changes value. Step Into - watch your program run line-by-line Rest the cursor over a variable to see its current value The Locals window - lists the current values of relevant
variables (before the line is executed) The Watches window - choose program variable
monitor, updated automatically as the program runs. The Command/Immediate window – try out
commands or change the value of variables while the program is running.• Type “?” followed by a variable or expression to see its value• Use “=“ to assign a new value to a variable
77
Trapping ErrorsTo prevent your program crashing - include error-trapping statements in your code. Validate user input
• If…..Then….Else….End If
Activate error-trapping to correct the error• On Error GoTo Label…..Resume
Use structured exception handling• Try….Catch….Finally
Provide helpful error messages
88
Validation
Any data input by the user should be validated to prevent the program crashing If….Then…..Else
99
Activate Error-TrappingTurn error-trapping on with a statement, so that if the system generates an error, the program jumps to the code after the label. On Error GoTo Label Identify end of normal processing by adding
“Exit Sub” just above End Sub Between Exit Sub and End Sub, place the Label
indicated by adding a colon (:) Following the label, insert the code to correct
the error. Finish with a “Resume” statement
1010
Activate Error-TrappingThe Resume statement has the following options: Resume - Re-execute the statement that caused the problem Resume Next - Continue the code following the problem
statement Resume Label2 – Go to the code following Label2
1111
Structured Exception HandlingA simpler method of Error-trapping is now available using the following format: Try
• Statements that may cause an error Catch condition1
• Code if condition1 is true Catch condition2
• Code if condition2 is true Finally (optional)
• Tidying-up statements (e.g. database error) End Try
1212
Structured Exception HandlingThis method provides the option of trapping various types of errors, and allows different messages to be given for each one.
It also avoids jumping up and down the procedure required by the On Error (unstructured) Method
1313
Logic Errors
Logic Errors occur when the program generates incorrect output or operates incorrectly.
These errors can go unnoticed, and even when they are detected, the source of the error can be difficult to diagnose.
The only reliable method for identifying this type of error is: Systematic and comprehensive Testing Check “expected output” against “actual output”