Show/Hide Toolbars

XSharp

Purpose

Define a sequence of statements for a BREAK.

Syntax

BEGIN SEQUENCE
 <Statements>...
[BREAK [<uValue>]]
 <Statements>...  
[RECOVER [USING <idVar>]]
 <Statements>...
END [SEQUENCE]

Arguments

BREAK <uValue>Branches execution to the statement immediately following the nearest RECOVER statement if one is specified, or the nearest END SEQUENCE statement.  <uValue> is the value returned into the <idVar> specified in the USING clause of the RECOVER statement.

 

RECOVER USING <idVar>
A recover point in the SEQUENCE construct where control branches after a BREAK statement.  If this clause is specified, <idVar> receives the value returned by the BREAK statement.  In general, this is an error object.  <idVar> must be a declared variable and cannot be strongly typed.

 

ENDThe end point of the SEQUENCE control structure.  If no RECOVER statement is specified, control branches to the first statement following the END statement after a BREAK.

Description

BEGIN SEQUENCE...END is a control structure used for exception and runtime error handling.  It delimits a block of statements defining a discrete operation, including invoked procedures and functions.  With the exception of the BREAK statement, the entire construct must fall within the same entity definition.

 

When a BREAK is encountered anywhere in a block of statements following the BEGIN SEQUENCE statement up to the corresponding RECOVER statement, control branches to the program statement immediately following the RECOVER statement.  If a RECOVER statement is not specified, control branches to the statement following the END statement, terminating the SEQUENCE.  If control reaches a RECOVER statement without encountering a BREAK, it branches to the statement following the corresponding END.

 

The RECOVER statement optionally receives a parameter passed by a BREAK statement that is specified with a return value.  This is usually an error object, generated and returned by the current error handling block defined by ErrorBlock().  If an error object is returned, it can be sent messages to query information about the error.  With this information, a runtime error can be handled within the context of the operation rather than in the current runtime error handler.

 

You cannot RETURN, LOOP, or EXIT between a BEGIN SEQUENCE and RECOVER statement

 

Control structures can be nested to any depth.  The only requirement is that each control structure be properly nested.

Examples

This code fragment demonstrates a SEQUENCE construct in which the BREAK occurs within the current procedure:

 

BEGIN SEQUENCE
  <Statements>...
  IF lBreakCond
    BREAK
  ENDIF
RECOVER
  <Recovery Statements>...
END
 
<Recovery Statements>...

 

This example demonstrates an error handler returning an error object to the variable specified in the USING clause of the RECOVER statement:

 

LOCAL oLocal, bLastHandler
// Save current and set new error handler
bLastHandler := ErrorBlock({|oErr| ;
     MyHandler(oErr, TRUE)})
 
BEGIN SEQUENCE
  .
  .  <Operation that might fail>...
  .
RECOVER USING oLocal
 
  // Send messages to oLocal & handle the error
  ? "Error:  "
  IF oLocal:GenCode != 0
     ?? oLocal:Description
  ENDIF
  .
  .  
  .
END
 
// Restore previous error handler
ErrorBlock(bLastHandler)
 
FUNCTION MyHandler(oError, lLocalHandler)
  // Handle locally returning the error object
  IF lLocalHandler
    BREAK oError
  ENDIF
 
  <Other statements to handle the error>...

 

This example re-executes a SEQUENCE statement block by issuing a LOOP from within the RECOVER statement block:

 

DO WHILE TRUE
  BEGIN SEQUENCE
 
     <Operation that may fail>...
 
  RECOVER
    IF PrintRecover()
          // Repeat the SEQUENCE statement block
        LOOP
    ENDIF
  END
  EXIT         // Escape from the operation
 
ENDDO

See Also

_Break(), CanBreak(), Error Class, ErrorBlock(), RETURN