Important: Please read the Qt Code of Conduct -

How to send an exception to the JavaScript context?

  • I have a simple Qt 4.7 application that uses QtWebKit to display a HTML/JavaScript page. Using addToJavaScriptWindowObject() a few C++ functions are provided to the JavaScript environment.

    Is there a way to raise an exception in the JavaScript context in certain situations when such an exported C++ function is called from JavaScript?

    • Throwing a C++ exception, like { throw 123; }, simply crashes the Qt application
    • Using @m_view->page()->mainFrame()->evaluateJavaScript("throw new Error('whatever');")@ I can throw an exception but apparently it is not being passed to the calling JavaScript context (i.e. just the eval() code itself is being aborted)

    Looking at the source code I see methods like setException() and similar but apparently I need a JSContext or some other reference to the calling JavaScript context, but I have no clue how to get it (there is no such thing in QWebFrame although WebKit itself or QWebFramePrivate seem to have such a reference).

    My final goal is that a JavaScript code like

    @try {
    } catch (e) {
    document . write("Exception: "+e.message);

    shows the exception created in C++ (specialBrowserObject being the object exposed via addToJavaScriptWindowObject()).

    Any suggestions?

Log in to reply