Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Error Handling In 1
  2. 2. Error TypesIn errors fall into one of two maincategories: Syntax Errors Runtime Errors Logic ErrorsEach type of error has a number of methodswhich can help the programmer identifyand fix the problem. This process is calledDebugging. 2
  3. 3. Syntax ErrorsSyntax (or compiler) errors occur when youwrite code that does not follow the rules ofthe language. When the compiler recognises a syntax error, it marks the error with a blue wavy line. 3
  4. 4. 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 4
  5. 5. Runtime ErrorsRuntime errors are the result of code thatdespite having the correct syntax, cannot beexecuted. if not handled, will generate an exception, that will cause the program to halt.This type of error is often caused by anunexpected variable value e.g. missing or invisible wrong data typeAnticipate code that may result in runtimeerrors and provide ways to avert crashes. 5
  6. 6. Debugging Runtime ErrorsUsing the debugger, accessed via the Debug menuor 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 6
  7. 7. Trapping ErrorsTo prevent your program crashing - includeerror-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 7
  8. 8. ValidationAny data input by the user should be validatedto prevent the program crashing If….Then…..Else 8
  9. 9. Activate Error-TrappingTurn error-trapping on with a statement, sothat if the system generates an error, theprogram 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 9
  10. 10. 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 10
  11. 11. Structured Exception HandlingA simpler method of Error-trapping is nowavailable 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 11
  12. 12. Structured Exception HandlingThis method provides the option of trappingvarious types of errors, and allows differentmessages to be given for each one.It also avoids jumping up and down theprocedure required by the On Error(unstructured) Method 12
  13. 13. Logic ErrorsLogic Errors occur when the programgenerates incorrect output or operatesincorrectly.These errors can go unnoticed, and evenwhen they are detected, the source of theerror can be difficult to diagnose.The only reliable method for identifying thistype of error is: Systematic and comprehensive Testing Check “expected output” against “actual output” 13