Error handling in visual fox pro 9

2,083
-1

Published on

Session on error handling I gave as a fill in at Southwest Fox I think.

1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total Views
2,083
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
23
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Error handling in visual fox pro 9

  1. 1. Error Handling in Visual FoxPro 9.0 Mike Feltman
  2. 2. Who Am I• President F1 Technologies• Visual FoxExpress Developer• Visual FoxPro Dinosaur• mikefeltman@f1tech.com• www.f1tech.com• www.f1technologies.blogspot.com
  3. 3. Agenda• Errors Happen• Types of Errors• Error Trapping Methods• Dealing with Errors• Combining Methods
  4. 4. Errors Happen • Virtually All Programs Have Errors • All Applications – even “bug free” ones - can encounter an error
  5. 5. Types of Errors• Coding Errors – Syntax Errors, Data Type Mismatch, Invalid # of Parameters, etc.• Environmental Errors – File Corruption, Printer not ready, Resource not Available, File not Found,• User Errors – Duplicate keys, invalid values
  6. 6. Types of Error Trapping• Native• ON ERROR• Error Method• TRY/CATCH• OTHER
  7. 7. Native Error Handling• At Design Time – Cancel, Ignore or Suspend• At Run Time – Cancel or Ignore
  8. 8. ON ERROR• Oldest method in the book• Format: – ON ERROR <action> – ON ERROR * – ON ERROR DO ErrorHandler WITH… – ON ERROR llError = .T. – ON ERROR goErrorHandler.DealWithIt(…)
  9. 9. ON ERROR• Pros • Cons – Centralized Mechanism – Removed from offending – Catch all code – Supports RETRY – Forces Monolithic Style – Supports RETURN of error handling – May require additional inline detection of error states in application code – Difficult to deal with specific scenarios
  10. 10. ON ERROR• Samples – Error1.prg – cErrorHandler of cError
  11. 11. The Error Event• Added in VFP 3.0• Native to all VFP Objects• Error(nError, cMethod, nLine)
  12. 12. The Error Event• Pros • Leads to duplicate code – Localized to offending • Can Require Lengthy DO object CASE constructs – Supports RETRY • Turns off ON ERROR error – Supports RETURN handling • May require additional inline detection of error states in application code
  13. 13. The Error Event• Samples – Error2.prg – cCursor of cData
  14. 14. TRY CATCH• Added in VFP 8.0• Structured Error Handling• Format – TRY – CATCH (WHEN) (TO) – THROW – FINALLY – ENDTRY
  15. 15. TRY CATCH• Pros • Cons – Deal w/ errors at the – Adapting Existing Apps source – Can make debugging – Less Cumbersome more difficult – Less Code in specific – No RETRY or RETURN TO routines – Easier recovery in some instances – Nests gracefully
  16. 16. The Exception Object & Catch• CATCH always creates a base class exception• Key Properties: ErrorNo Procedure Message UserValue LineContents StackLevel Details
  17. 17. The Throw Command• THROW <expression>• Creates an exception• Expression is placed in user value• Can be used any time a TRY CATCH is in Effect• SYS(2410) can be used to detect TRY/CATCH
  18. 18. TRY CATCH• SAMPLES• Error3 – Error9
  19. 19. OTHER ERROR TRAPPING• TABLEUPDATE() ERRORS – Field Rule Violated 1582 – Record Rule Violated 1583 – Trigger Failed 1539 – Unique Index Violated 1884 – Update Conflict 1585• SQLEXEC(), SQLCOMMIT(), SQLCONNECT(), etc. all populate the AERROR array.
  20. 20. Connectivity Errors• Connectivity Error 1526• AERROR() will contain the back-end error(s) – Column 3: ODBC Error Message – Column 4: ODBC State – Column 5: ODBC Error Number – Column 6: Connection Handle• AERROR() can return multiple rows
  21. 21. Mixing & Matching• If error is w/in object error event takes precedence• If error is in TRY/CATCH structured error handling takes precedence• If error is in procedural code ON ERROR takes precedence• Samples: Error10 - 16
  22. 22. Related Commands & Functions• Error Details • More Info – MESSAGE(), MESSAGE(1) – LIST STATUS – SYS(2018) – LIST MEMORY – AERROR() – LIST OBJECTS• Error Location • Environment – LINENO() – SYS(5) + CURDIR() – PROGRAM(), SYS(16) – SET(“PATH”) – ACALLSTACKINFO() – SET(“CLASSLIB”) – SET(“PROCEDURE”) – SYS(2019) – OS() – Version() – SYS(0)
  23. 23. Conclusion• VFP Has a lot of ways to deal with errors• Mixing and Matching the techniques lets you take advantage of all of them• More Info: – VFP Best Practices from Henztenwerke (Rick Schummer) – Structured Error Handling Topic on Fox.wikis.com by Randy Pearson – CoDe Focus VFP 8 Issue (CodeMagazine.Com)
  24. 24. Thank you!• mikefeltman@f1tech.com• www.f1tech.com• www.f1technologies.blogspot.com• 419-255-6366

×