Skip to content Skip to sidebar Skip to footer

Catching Exceptions In Javascript Thrown From Activex Control Written In C++

I've written an ActiveX control in C++ that throws (C++) exceptions out when error conditions occur within the control. The Javascript code that invokes the object representing an

Solution 1:

How are you passing the exception from C++? A general throw does not work if you want to propagate your exception to javascript. You need to throw exception of type COleDispatchException and the right way to do is calling

AfxThrowOleDispatchException(101, _T("Exception Text Here")); // First parameter is exception code. 

Reference: http://doc.sumy.ua/prog/active_x/ch03.htm#Heading20

Solution 2:

You need AtlReportError. It throws javascript exception with description string:

STDMETHODIMP CMyCtrl::MyMethod()
{
   ...
   if (bSucceeded)
      return S_OK;
   else// hRes is set to DISP_E_EXCEPTIONreturn AtlReportError (GetObjectCLSID(), "My error message");
}

Solution 3:

You can't pass C++ exceptions to the script - you need to catch the C++ exceptions in Invoke()/InvokeEx(), translate them and pass them out using the EXCEPINFO* parameter.

E.g. excerpting from FireBreath'simplementation:

HRESULT YourIDispatchExImpl::InvokeEx(DISPID id, LCID lcid, WORD wFlags, 
                                      DISPPARAMS *pdp, VARIANT *pvarRes, 
                                      EXCEPINFO *pei, IServiceProvider *pspCaller)
{
    try {
        // do actual work
    } catch (const cppException& e) {
        if (pei != NULL) {
            pei->bstrSource = CComBSTR(ACTIVEX_PROGID);
            pei->bstrDescription = CComBSTR(e.what());
            // ...
        }
        return DISP_E_EXCEPTION;
    }

    // ...

Post a Comment for "Catching Exceptions In Javascript Thrown From Activex Control Written In C++"