xHarbour Reference Documentation > Statement Reference |
Declares a control structure for error handling.
BEGIN SEQUENCE <statements_Normal> [ BREAK [<expression>] ] [ RECOVER [USING <errorVar>] <statements_Error> ] END[SEQUENCE]
BEGIN SEQUENCE starts the declaration of an error handling control structure. It is a Clipper compatible control structure that is superseded with xHarbour's TRY...CATCH control structure.
The statements BEGIN SEQUENCE and ENDSEQUENCE determine a sequence of statements in PRG code where the programmer expects a runtime error to occur. The runtime error is gracefully handled in the routine defined with the error codeblock. The default error codeblock is defined in ERRORSYS.PRG.
If a runtime error occurs in a program between BEGIN SEQUENCE and ENDSEQUENCE, program flow is controlled by the BREAK statement or the Break() function. Both send an error recovery value to the RECOVER USING <errorVar> statement, where <errorVar> receives the value of the expression passed to Break() or BREAK. The error recovery value is usually an error object.
When the RECOVER statement is absent within BEGIN SEQUENCE and ENDSEQUENCE, and a runtime error occurs, a program resumes with the statement following ENDSEQUENCE.
Note: It is not possible to use the EXIT, LOOP or RETURN statements within the <statements_Normal> block of statements. Program flow cannot leave the sequence of statements declared with BEGIN SEQUENCE unless the RECOVER or ENDSEQUENCE statement is reached.
See also: | Break(), ErrorBlock(), ErrorNew(), RETURN, TRY...CATCH |
Category: | Control structures , Statements |
// The example demonstrates a typical situation where BEGIN SQEUENCE // is used. A database file is opened without testing the existence // of an index file. When the index file does not exist, it is created // in the RECOVER section of the control structure. PROCEDURE Main CLS OpenDatabase() WAIT Browse() DbCloseAll() RETURN PROCEDURE OpenDatabase LOCAL bError := ErrorBlock( {|e| Break(e) } ) DO WHILE .T. BEGIN SEQUENCE // Try to open the database with index USE Customer ALIAS Cust INDEX CustA.ntx SHARED RECOVER // Error: index missing, Build index USE Customer ALIAS Cust EXCLUSIVE INDEX ON Upper(LastName) TO CustA.ntx USE LOOP ENDSEQUENCE EXIT ENDDO ErrorBlock( bError ) RETURN
http://www.xHarbour.com