Error handling in visual fox pro 9

Post on 30-Apr-2015

2,651 views 12 download

description

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

transcript

Error Handling in Visual Error Handling in Visual FoxPro 9.0FoxPro 9.0

Mike FeltmanMike Feltman

Who Am IWho Am I• President F1 TechnologiesPresident F1 Technologies• Visual FoxExpress DeveloperVisual FoxExpress Developer• Visual FoxPro DinosaurVisual FoxPro Dinosaur• mikefeltman@f1tech.commikefeltman@f1tech.com• www.f1tech.comwww.f1tech.com • www.f1technologies.blogspot.comwww.f1technologies.blogspot.com

AgendaAgenda• Errors HappenErrors Happen• Types of ErrorsTypes of Errors• Error Trapping MethodsError Trapping Methods• Dealing with ErrorsDealing with Errors• Combining MethodsCombining Methods

Errors HappenErrors Happen• Virtually All Programs Virtually All Programs

Have ErrorsHave Errors• All Applications – even All Applications – even

“bug free” ones - can “bug free” ones - can encounter an errorencounter an error

Types of ErrorsTypes of Errors• Coding ErrorsCoding Errors

– Syntax Errors, Data Type Mismatch, Invalid # Syntax Errors, Data Type Mismatch, Invalid # of Parameters, etc. of Parameters, etc.

• Environmental ErrorsEnvironmental Errors– File Corruption, Printer not ready, Resource not File Corruption, Printer not ready, Resource not

Available, File not Found, Available, File not Found,

• User ErrorsUser Errors– Duplicate keys, invalid valuesDuplicate keys, invalid values

Types of Error TrappingTypes of Error Trapping• NativeNative• ON ERRORON ERROR• Error MethodError Method• TRY/CATCHTRY/CATCH• OTHEROTHER

Native Error HandlingNative Error Handling• At Design Time – Cancel, Ignore or At Design Time – Cancel, Ignore or

SuspendSuspend• At Run Time – Cancel or IgnoreAt Run Time – Cancel or Ignore

ON ERRORON ERROR• Oldest method in the bookOldest method in the book• Format: Format:

– ON ERROR <action>ON ERROR <action>– ON ERROR *ON ERROR *– ON ERROR DO ErrorHandler WITH…ON ERROR DO ErrorHandler WITH…– ON ERROR llError = .T.ON ERROR llError = .T.– ON ERROR ON ERROR

goErrorHandler.DealWithIt(…)goErrorHandler.DealWithIt(…)

ON ERRORON ERROR• ProsPros

– Centralized Centralized MechanismMechanism

– Catch allCatch all– Supports RETRYSupports RETRY– Supports RETURNSupports RETURN

• ConsCons– Removed from Removed from

offending codeoffending code– Forces Monolithic Forces Monolithic

Style of error handlingStyle of error handling– May require additional May require additional

inline detection of inline detection of error states in error states in application codeapplication code

– Difficult to deal with Difficult to deal with specific scenariosspecific scenarios

ON ERRORON ERROR• SamplesSamples

– Error1.prgError1.prg– cErrorHandler of cErrorcErrorHandler of cError

The Error EventThe Error Event• Added in VFP 3.0Added in VFP 3.0• Native to all VFP ObjectsNative to all VFP Objects• Error(nError, cMethod, nLine)Error(nError, cMethod, nLine)

The Error EventThe Error Event• ProsPros

– Localized to Localized to offending objectoffending object

– Supports RETRYSupports RETRY– Supports RETURNSupports RETURN

• Leads to duplicate Leads to duplicate codecode

• Can Require Lengthy Can Require Lengthy DO CASE constructsDO CASE constructs

• Turns off ON ERROR Turns off ON ERROR error handlingerror handling

• May require May require additional inline additional inline detection of error detection of error states in application states in application codecode

The Error EventThe Error Event• SamplesSamples

– Error2.prgError2.prg– cCursor of cDatacCursor of cData

TRY CATCHTRY CATCH

• Added in VFP 8.0Added in VFP 8.0• Structured Error HandlingStructured Error Handling• Format Format

– TRYTRY– CATCH (WHEN) (TO)CATCH (WHEN) (TO)– THROWTHROW– FINALLYFINALLY– ENDTRYENDTRY

TRY CATCHTRY CATCH• ProsPros

– Deal w/ errors at Deal w/ errors at the sourcethe source

– Less CumbersomeLess Cumbersome– Less Code in Less Code in

specific routinesspecific routines– Easier recovery in Easier recovery in

some instancessome instances– Nests gracefullyNests gracefully

• ConsCons– Adapting Existing Adapting Existing

AppsApps– Can make Can make

debugging more debugging more difficultdifficult

– No RETRY or No RETRY or RETURN TORETURN TO

The Exception Object & The Exception Object & CatchCatch

• CATCH always creates a base class CATCH always creates a base class exceptionexception

• Key Properties:Key Properties:ErrorNoErrorNo ProcedureProcedure

MessageMessage UserValueUserValue

LineContentsLineContents StackLevelStackLevel

DetailsDetails

The Throw CommandThe Throw Command• THROW <expression>THROW <expression>• Creates an exceptionCreates an exception• Expression is placed in user valueExpression is placed in user value• Can be used any time a TRY CATCH Can be used any time a TRY CATCH

is in Effectis in Effect• SYS(2410) can be used to detect SYS(2410) can be used to detect

TRY/CATCHTRY/CATCH

TRY CATCHTRY CATCH• SAMPLESSAMPLES• Error3 – Error9Error3 – Error9

OTHER ERROR TRAPPINGOTHER ERROR TRAPPING• TABLEUPDATE() ERRORSTABLEUPDATE() ERRORS

– Field Rule Violated 1582Field Rule Violated 1582– Record Rule Violated 1583Record Rule Violated 1583– Trigger Failed 1539Trigger Failed 1539– Unique Index Violated 1884Unique Index Violated 1884– Update Conflict 1585Update Conflict 1585

• SQLEXEC(), SQLCOMMIT(), SQLEXEC(), SQLCOMMIT(), SQLCONNECT(), etc. all populate the SQLCONNECT(), etc. all populate the AERROR array.AERROR array.

Connectivity ErrorsConnectivity Errors• Connectivity Error 1526Connectivity Error 1526• AERROR() will contain the back-end AERROR() will contain the back-end

error(s)error(s)– Column 3: ODBC Error MessageColumn 3: ODBC Error Message– Column 4: ODBC StateColumn 4: ODBC State– Column 5: ODBC Error NumberColumn 5: ODBC Error Number– Column 6: Connection HandleColumn 6: Connection Handle

• AERROR() can return multiple rowsAERROR() can return multiple rows

Mixing & MatchingMixing & Matching• If error is w/in object error event If error is w/in object error event

takes precedencetakes precedence• If error is in TRY/CATCH structured If error is in TRY/CATCH structured

error handling takes precedenceerror handling takes precedence• If error is in procedural code ON If error is in procedural code ON

ERROR takes precedenceERROR takes precedence• Samples: Error10 - 16Samples: Error10 - 16

Related Commands & Related Commands & FunctionsFunctions

• Error DetailsError Details– MESSAGE(), MESSAGE(),

MESSAGE(1)MESSAGE(1)– SYS(2018)SYS(2018)– AERROR()AERROR()

• Error LocationError Location– LINENO()LINENO()– PROGRAM(), SYS(16)PROGRAM(), SYS(16)– ACALLSTACKINFO()ACALLSTACKINFO()

• More InfoMore Info– LIST STATUSLIST STATUS– LIST MEMORYLIST MEMORY– LIST OBJECTSLIST OBJECTS

• EnvironmentEnvironment– SYS(5) + CURDIR()SYS(5) + CURDIR()– SET(“PATH”)SET(“PATH”)– SET(“CLASSLIB”)SET(“CLASSLIB”)– SET(“PROCEDURE”)SET(“PROCEDURE”)– SYS(2019)SYS(2019)– OS()OS()– Version()Version()– SYS(0)SYS(0)

ConclusionConclusion• VFP Has a lot of ways to deal with errorsVFP Has a lot of ways to deal with errors• Mixing and Matching the techniques lets Mixing and Matching the techniques lets

you take advantage of all of themyou take advantage of all of them• More Info:More Info:

– VFP Best Practices from Henztenwerke (Rick VFP Best Practices from Henztenwerke (Rick Schummer)Schummer)

– Structured Error Handling Topic on Structured Error Handling Topic on Fox.wikis.com by Randy PearsonFox.wikis.com by Randy Pearson

– CoDe Focus VFP 8 Issue CoDe Focus VFP 8 Issue (CodeMagazine.Com)(CodeMagazine.Com)

Thank you!Thank you!

• mikefeltman@f1tech.commikefeltman@f1tech.com• www.f1tech.comwww.f1tech.com • www.f1technologies.blogspot.comwww.f1technologies.blogspot.com• 419-255-6366419-255-6366