xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

ErrorBlock()

Sets or retrieves the error handling code block.

Syntax

ErrorBlock( [<bErrorBlock>] ) --> bLastErrorBlock

Arguments

<bErrorBlock>
An optional code block can be passed that replaces the current error handling code block. The code block receives as parameter an Error() object containing information about a runtime error.

Return

The return value is the currently installed error handling code block.

Description

The error code block is part of the runtime error handling system of xHarbour and can be retrieved or defined with the ErrorBlock() function. A default error code block is installed at program startup in the ErrorSys() routine.

By default, the error code block is evaluated when a runtime error occurs ouside a TRY...CATCH control structure. An error object is filled with error information by the xHarbour runtime kernel and passed on to the error code block. The error code block can then call a user defined error handling routine, or pass the Error object on to the Break() function, which, in turn, passes the Error object on to the RECOVER option of the BEGIN SEQUENCE statement.

If the error code block does not call Break(), its return value must be of logical data type to instruct the error handling system how to proceed with error handling. If .T. (true) is returned, the offending operation is retried, while .F. (false) instructs the error handling system to ignore the error and resume processing.

Note:  the error code block is part of xHarbour's Clipper compatible error handling system and not recommended to use in new applications. Instead, xHarbour's moder TRY...CATCH error handling control structure provides a modern and more flexible way for programming runtime error recovery.

Info

See also:BEGIN SEQUENCE, Break(), ErrorNew(), ErrorSys(), TRY...CATCH
Category: Error functions , Debug functions
Source:vm\errorapi.c
LIB:xhb.lib
DLL:xhbdll.dll

Examples

// This example uses offensive error handling by assuming that
// index files required for a database exist. If they do not
// exist, they are created in the RECOVER section of the BEGIN
// SEQUENCE statement.

   PROCEDURE Main
      OpenCustomer()

      ? "Files are open"
      ? "Program can start"
   RETURN


   PROCEDURE OpenCustomer
      LOCAL oError, n
      LOCAL bError := ErrorBlock( {|e| Break(e) } )

      FOR n := 1 TO 2
         BEGIN SEQUENCE
            // regular code
            USE Customer NEW SHARED
            SET INDEX TO Cust01, Cust02
            ErrorBlock( bError )

         RECOVER USING oError
            // error handling code
            ErrorBlock( bError )

            USE Customer NEW EXCLUSIVE
            INDEX ON CustNo TO Cust01
            INDEX ON Upper(LastName+FirstName) TO Cust02
            USE

            // go back to open DBF in SHARED mode
            LOOP
        ENDSEQUENCE

        EXIT
     NEXT

   RETURN

 

// This example does the same as the previous one, except that
// it uses TRY...CATCH instead of an error code block.

   PROCEDURE Main
      OpenCustomer()

      ? "Files are open"
      ? "Program can start"
   RETURN


   PROCEDURE OpenCustomer
      LOCAL n

      FOR n:=1 TO 2
         TRY
            // regular code
            USE Customer NEW SHARED
            SET INDEX TO Cust01, Cust02
         CATCH
            // error recovery
            USE Customer NEW EXCLUSIVE
            INDEX ON CustNo TO Cust01
            INDEX ON Upper(LastName+FirstName) TO Cust02
            USE
            LOOP
         END

         EXIT
      NEXT

   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe