• No results found

BEGIN SEQUENCE

In document xHarbour Language Reference Guide (Page 96-99)

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.

BEGIN SEQUENCE 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

CLASS

CLASS

Declares the class function of a user-defined class.

Syntax

CLASS <ClassName> [ FROM <SuperClass,...> ] [ STATIC ] [EXPORTED: | PROTECTED: | HIDDEN:]

<Member declarations>

<...>

ENDCLASS

Arguments

<ClassName>

This is the symbolic name of the class to declare. It must begin with a letter or underscore followed by digits, letters or underscores. The symbolic name can contain up to 63 characters.

FROM <SuperClass,...>

The FROM option specifies one or more classes the new class is derived from. The names of these classes must be comma separated and must exist in an application. <ClassName> inherits all member variables and methods from its super class(es). A synonym for FROM is INHERIT (CLASS <ClassName> INHERIT <SuperClass,...>)

STATIC

When this option is specified, the class function is created asSTATIC FUNCTION. In this case, objects of the class can only be created in the PRG module declaring the class.

EXPORTED: | PROTECTED: | HIDDEN:

The EXPORTED:, PROTECTED: and HIDDEN: options modify the visibility attribute for the member variables and methods of the class. The default attribute is EXPORTED:. See an explanation for the visibility attribute in the description.

Note that the colon is mandatory at the end of this option. If it is missing, the compiler reports a syntax error.

<Member declarations>

Member variables and methods of the class must be declared separately using the statements DATAandMETHOD. These declarations define the symbolic names for accessing member variables and executing methods for objects of a class.

ENDCLASS

This terminates the class declaration and is mandatory. If the CLASS declaration does not end with ENDCLASS, the compiler reports a syntax error.

Description

The CLASS statement declares the Class function of a user-defined class. This function is required to instatiate a class, i.e. to create objects of a class. Objects are always created with the :new() method, which is part of the abstract base classHBObject()of xHarbour's OOP model.

The CLASS declaration ends with ENDCLASS. Between CLASS and ENDCLASS, the symbolic names for the members of a class must be declared withDATA(instance variables) and/orMETHOD

CLASS (instance methods) and/orCLASSDATA(class variables) and/orCLASSMETHOD(class methods).

When the class declaration is complete, declared methods must be implemented with another METHODdeclaration. It is an error when a method is declared but not implemented, and vice versa.

Inheritance

A new class can be declared on the basis of existing classes. The name of one or more classes is specified with the FROM option. In this case, the new declared class inherits all members of

<SuperClass,...> and can access them unless their visibility attribute is set to HIDDEN:.

If no superclass is specified,HBObject()is used as implicit superclass, i.e. all methods of HBObject() exist in all user-defined classes.

In document xHarbour Language Reference Guide (Page 96-99)