KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Use a wrapper method to add efficiency to ON ERR CALL
PRODUCT: 4D | VERSION: 16/15/14/13/12/11 | PLATFORM: Mac & Win
Published On: December 2, 2016

It is easy and good programming practice to use "good neighbor" coding when writing 4D methods. "Good neighbor" programming means, whenever possible, capture the state of the program at the start of a method and reinstating the state at the end of a method.

An excellent example is the use of a wrapper to add efficiency and consistency to the use of the command ON ERR CALL.

The code snippet below is the wrapper that implements the "good neighbor" programming when it comes to using the ON ERR CALL 4D command. It captures the name of the installed method using Method called on error and returns the name of any installed error method so that it can be reinstated appropriately at the end of the method.

//--------------------------------------------------------------------------
//method_parameters_declarations
C_TEXT($0;$CurrentMethod_T)
C_TEXT(;$NewMethod_T;$1)
//--------------------------------------------------------------------------
//local_variable_declarations
C_LONGINT($Params_L)

//====================== Initialize and Setup ================================

$Params_L:=Count parameters
$CurrentMethod_T:=""
If ($Params_L>0)
    $NewMethod_T:=$1
   
    //======================== Method Actions ==================================
   
    $CurrentMethod_T:=Method called on error
    ON ERR CALL($NewMethod_T)
   
    //======================== Clean up and Exit =================================
   
End if

$0:=$CurrentMethod_T

Example usage of the wrapper is shown below.

C_TEXT($OnErrMethod_T)
$OnErrMethod_T:=On_err_call ("AnotherOnErrMethod")
// Do something...
ON ERR CALL($OnErrMethod_T)