Click or drag to resize

ErrorBlock Function

X#
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.19
Syntax
 FUNCTION ErrorBlock() AS USUAL
Request Example View Source

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 forO:
X#
1{|<paramref name="oError" />| <paramref name="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#
 1LOCAL cbErrorHandler, cbLastHandler, oError
 2cbErrorHandler := {|oError|MyErrorHandler(oError)}
 3                            // Save current handler
 4cbLastHandler := ErrorBlock(cbErrorHandler)
 5BEGIN SEQUENCE
 6    .
 7    . <paramref name="OperationStatements" />
 8    .
 9                            // Receive error object from BREAK
10RECOVER USING oErrorInfo
11    .
12    . <paramref name="RecoveryStatements" />
13    .
14END
15ErrorBlock(cbLastHandler)            // Restore handler
16FUNCTION MyErrorHandler(oError)
17    BREAK oError
18                            // 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:
See Also