BEGIN SEQUENCE
tryStatements
[RECOVER [USING localVariable]
recoveryStatements
]
[FINALLY
finallyStatements
]
END [SEQUENCE]
where:
tryStatements | One or more statements or expressions that may cause an exception to be thrown. |
localVariable | A local variable that will receive the exception thrown by any code between the BEGIN SEQUENCE and RECOVER statements. This must be a variable typed as USUAL. |
recoveryStatements | One or more statements or expressions that will execute if an exception is thrown by any of the tryStatements. |
finallyStatements | One or more statements or expressions that will always execute regardless of whether an exception is thrown or not. |
BEGIN SEQUENCE ... END SEQUENCE 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.
When an exception is thrown anywhere anywhere in the 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 FINALLY statement, terminating the sequence. If a FINALLY statement is not specified, control branches to the statement following the END SEQUENCE statement, terminating the sequence.
If control reaches a RECOVER statement without an exception being thrown, control branches to the statement following the FINALLY statement. If a FINALLY statement is not specified, control branches to the statement following the END SEQUENCE statement, terminating the sequence.
The RECOVER statement optionally receives an exception thrown by a statement in the tryStatements block. 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.
The FINALLY statement block is useful for cleaning up any resources allocated in the BEGIN SEQUENCE block. Control is always passed to the FINALLY block (if present) regardless of how the BEGIN SEQUENCE block exits.
You cannot RETURN, LOOP, or EXIT between a BEGIN SEQUENCE and RECOVER statement. From within the RECOVER and FINALLY statement blocks however, you can LOOP, EXIT, BREAK, or RETURN since the sequence is essentially completed at that point. Using LOOP from within the RECOVER statement block is useful for re-executing the sequence statement block.
BEGIN SEQUENCE ... END SEQUENCE control structures can be nested to any depth.
The CanBreak() function returns TRUE if execution is within any BEGIN SEQUENCE block.
FUNCTION Start() AS VOID
LOCAL x := 4, y := 0 AS INT
BEGIN SEQUENCE
? x / y
RECOVER
? "oops"
FINALLY
? "in finally block"
END SEQUENCE
RETURN