XLL+ Class Library (7.0)

Overriding exception-handling behavior

You can override the virtual function CXllApp::DisplayException() to change the exception-handling behavior of all functions in the add-in.

Converting strings to error values

In particular, you may wish to convert all instances of a particular class of exception to a particular Excel error value, such as #VALUE!. The library function CXllApp::ConvertTypeToError can be used to do the hard work, along with the XLEXCEPTION_TRANSLATION macro.

CopyC++
XlExceptionTranslation translations[] = {
    XLEXCEPTION_TRANSLATION(CXlConversionException, xlerrValue),
    XLEXCEPTION_TRANSLATION_END
};

void CTutorial1App::DisplayException(CXlOper& xloResult, const CXlRuntimeException& ex)
{
    // Try the conversion list 
    if (ConvertTypeToError(translations, xloResult, ex))
        return;
    // Revert to the base class for the standard handler
    CXllApp::DisplayException(xloResult, ex);
}

XLEXCEPTION_TRANSLATION(type, xlerr)

This macro defines a translation pair containing:

  1. the class name of the exception type
  2. the Excel error value to which all conversions of this type will be translated

The macro will cause a compiler error if the class name is misspelt; this behavior is intentional and avoids the possibility of errors that might occur if you merely provide the name of the exception type as a string.

Enable Run-time Type Info

If you use the XLEXCEPTION_TRANSLATION macro, you should ensure that your project has the Enable Run-time Type Info setting set to Yes (/GR). The setting can be found on the C/C++ - Language page of the project settings.

Changing the format

You may want to change the default format of error messages, which is to wrap the exception's error message as follows:

#ERROR: [message]

The code below changes the format to #VALUE! [message].

CopyC++
void CTutorial1App::DisplayException(CXlOper& xloResult, const CXlRuntimeException& ex)
{
    CString fullMessage;
    fullMessage.Format("#VALUE! %s", ex.what());
    xloResult = fullMessage;    
}

Next: C Runtime Exceptions >>