Occasionally it may make sense to also take advantage of structured exception handling in a page. Visual FoxPro 8.0 provides the in-built commands TRY…CATCH…ENDTRY for this purpose. In all versions supported by AFP (including Visual FoxPro 7.0) you have access to the _BEGIN… …_END construction in the .code file:
_BEGIN
_TRY
DoSomethingInAFunction()
_CATCH
__EXCEPTION.Report()
_END
In AFP, the _TRY may only consist of one line - in contrast to the Visual FoxPro 8.0 variant. If you execute multiple lines you will have to declare a function to be invoked. The _END must always be the last line in a procedure; this limits the number of _TRY blocks to one. If a further error occurs after _END or within _CATCH, AFP will come to a halt. This is the reason why you should use this construction only inside a specially written procedure.
In addition, you can solve the majority of applications through App.TryRepeat. This method lets you call a function. In case this triggers an error the function will be invoked up to ten times in irregular intervals. This may make sense to obtain access to certain files:
Local lnFileHandle
If App.TryRepeat("OpenFile",@lnFileHandle,"Customer.txt")
Fwrite( m.lnFileHandle, "Hello world"+Chr(13)+Chr(10) )
EndIf
Procedure OpenFile( tcFile )
Return Fcreate( m.tcFile )
If multiple instances attempt to simultaneously access this file, there is a better chance that access will indeed be possible. Structured error handling in its AFP variant is also equipped with an equivalent to THROW. You may use the _THROW command in the files .afp or .afp.code .
_THROW "Cannot find file"
In your own PRG and VCX classes you can use the Server.Throw() method:
Server.Throw("File not found")
In both cases the error HTTP 500 "Internal Server Error" will be triggered but not the error event. You can achieve this through the command ERROR which will let you trigger any error you want.