Click or drag to resize

ErrorBlock Function (Codeblock)

Return and optionally change the code block that is executed when a runtime error occurs.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.10
Syntax
 FUNCTION ErrorBlock(
	cbNewSetting AS Codeblock
) AS USUAL
View Source

Parameters

cbNewSetting
Type: Codeblock
The code block to execute whenever a runtime error occurs.

Return Value

Type: Usual
If <cbNewSetting> is not specified, ErrorBlock() returns the current setting.  If <cbNewSetting> is specified, the previous setting is returned.
Remarks
X# defines a default error handling code block that determines what to do when a runtime error occurs.  ErrorBlock() lets you override this default error handling mechanism with one that you define. Specify the error handler as a code block with the following form:
X#
{|<oError>| <uList>}
where <oError> is an error object containing information about the error and <uList> is a comma-separated list of USUAL expressions that call the function that actually handles the errors.  Within the code block, messages can be sent to the error object to obtain information about the error.  A return value of TRUE from the error handling code block retries the failed operation, whereas a return value of FALSE resumes processing. The error handling code block can be specified either as a list of expressions or as a call to a function.  A call to a function is more useful since you can use X# control statements instead of expressions.  This is particularly the case if there is a BEGIN SEQUENCE or LabelPush() pending and you want to BREAK to the nearest RECOVER statement. As this implies, error handling code blocks can be used in combination with BEGIN SEQUENCE...END control structures.  Within an error handling code block, you handle device, low-level, and common errors that have a general recovery mechanism.  If the operation needs specific error handling, define a BEGIN SEQUENCE or LabelPush(), then BREAK to the RECOVER statement, returning the error object for local processing.  See the example below. For more information on the structure and operations of error objects, refer to the Error Class entry.
Examples
This code fragment posts, then calls an error handling code block when there is an error within a BEGIN SEQUENCE construct:
X#
LOCAL cbErrorHandler, cbLastHandler, oError
cbErrorHandler := {|oError|MyErrorHandler(oError)}
                                                        // Save current handler
cbLastHandler := ErrorBlock(cbErrorHandler)
BEGIN SEQUENCE
        .
        . <OperationStatements>
        .
                                                        // Receive error object from BREAK
RECOVER USING oErrorInfo
        .
        . <RecoveryStatements>
        .
END
ErrorBlock(cbLastHandler)                        // Restore handler
FUNCTION MyErrorHandler(oError)
        BREAK oError
                                                        // Return error object to RECOVER
This example improves on the previous example.  Instead of setting up ErrorBlock({|oError| _Break(oError)}) which just does a BREAK without taking advantage of the facilities of the error system (for example, VOERROR.LOG), you could set up an Init method for the Error class as follows:
X#
METHOD Init() CLASS Error
        SELF:Choice :=EC_BREAK
This way you always get a BREAK after successfully running the error system and also update VOERROR.LOG.  EC_BREAK is already defined by the system.
See Also