Skip to content

COM Error Handling

Error Handling

When an error occurs in a COM object (e.g., a method invocation failed), the control may throw a COM exception and return an error code. When this happens, FlexCOM raises a DataFlex error. This allows you to trap the error using a standard DataFlex error handler and handle it in an appropriate way or optionally suppress the error.

The following example demonstrates how to write DataFlex code to handle errors in a currency converter COM object (cComEDConverter). The function IsExchangeRateAvailable tests if the passed currency symbol is available by executing the ComGetExchangeRate method and trapping any errors raised and returned by the COM method.

Object oEDConverter is a cComEDConverter
    Property Boolean pbErrorFree True

    Procedure Error_Report Integer iErrNum ;
        Integer iErrLine String sErrMsg
        // Internal error handler to suppress error messages and set
        // an internal flag.
        Set pbErrorFree To False
    End_Procedure

    Function IsExchangeRateAvailable String sCurrencySymbol ;
        Returns Boolean
        // Tests if the exchange rate is available. This works by
        // trapping the returned error code from ComGetExchangeRate.
        Integer hoOldErrorId
        Boolean bErrFree
        Real rExchangeRate

        // Re-route DataFlex errors to internal Error_Report Procedure.
        Move Error_Object_ID To hoOldErrorId
        Move Self To Error_Object_ID

        // Send ComGetExchangeRate and test for any returned errors.
        Set pbErrorFree To True
        Get ComGetExchangeRate sCurrencySymbol To rExchangeRate
        Get pbErrorFree To bErrFree

        // Reset the DataFlex error handler.
        Move hoOldErrorId To Error_Object_ID
        Function_Return bErrFree
    End_Function // IsExchangeRateAvailable
End_Object

Note that the techniques used here are identical to the error handling techniques you would use in any DataFlex code.

See Also