This document discusses error handling in Visual FoxPro 9.0. It describes different types of errors that can occur, such as coding errors, environmental errors, and user errors. It then explains various methods for handling errors in Visual FoxPro, including native error handling, using ON ERROR, the Error method, TRY/CATCH blocks, and other techniques. Examples are provided to illustrate how to implement each error handling method. Related commands and functions for retrieving error details are also outlined.
2. Who Am I
• President F1 Technologies
• Visual FoxExpress Developer
• Visual FoxPro Dinosaur
• mikefeltman@f1tech.com
• www.f1tech.com
• www.f1technologies.blogspot.com
3. Agenda
• Errors Happen
• Types of Errors
• Error Trapping Methods
• Dealing with Errors
• Combining Methods
4. Errors Happen
• Virtually All Programs Have
Errors
• All Applications – even “bug
free” ones - can encounter an
error
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. Types of Error Trapping
• Native
• ON ERROR
• Error Method
• TRY/CATCH
• OTHER
7. Native Error Handling
• At Design Time – Cancel, Ignore or Suspend
• At Run Time – Cancel or Ignore
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. 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
11. The Error Event
• Added in VFP 3.0
• Native to all VFP Objects
• Error(nError, cMethod, nLine)
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
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. The Exception Object & Catch
• CATCH always creates a base class exception
• Key Properties:
ErrorNo Procedure
Message UserValue
LineContents StackLevel
Details
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
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. 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. 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. 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. 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)