xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

BEGIN SEQUENCE

Declares a control structure for error handling.

Syntax

BEGIN SEQUENCE
 <statements_Normal>

 [ BREAK [<expression>] ]

 [ RECOVER [USING <errorVar>]
   <statements_Error>
 ]
END[SEQUENCE]

Arguments

<statements_Normal>
The statements to execute when no error occurs.
BREAK [<expression>]
When a BREAK statement or the Break() function is executed within a BEGIN SEQUENCE block, program flow branches to the next statement following the RECOVER statement. The value <expression> passed to BREAK or the Break() function is assigned to <errorVar>, if specified.
RECOVER [USING <errorVar>]
The RECOVER statement specifies the point in a program where it resumes execution if a runtime error occurs. Optionally, a variable <errorVar> can be given that receives the value passed to BREAK or the Break() function.
<statements_Error>
The statements to execute when an error occurs and a BREAK or Break() is executed.

Description

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.

Info

See also:Break(), ErrorBlock(), ErrorNew(), RETURN, TRY...CATCH
Category: Control structures , Statements

Example

// 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

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe