JS_IsExceptionPending

Determine whether an exception is pending in the JS engine.

Syntax

bool
JS_IsExceptionPending(JSContext *cx);
Name Type Description
cx JSContext * Pointer to a JS context to check for pending exceptions.

Description

JS_IsExceptionPending returns true if an exception has been thrown in the context cx and the exception has not yet been caught or cleared. Otherwise, it returns false

This can be used from JSNative functions which call JS code to determine if the called JS code threw an exception or not.

Example

/* JSAPI */

bool pending;
JS::RootedValue exception(cx);

/* If an exception is pending, save and clear it. */
pending = JS_IsExceptionPending(cx);
if (pending) {
    if (!JS_GetPendingException(cx, &exception))
        return false;
    JS_ClearPendingException(cx);
}

/* ... code that might throw JS exception ... */

if (JS_IsExceptionPending(cx)) {

    /* ... react to exception thrown in this block ... */

    JS_ClearPendingException(cx);
}

/* Restore the original exception. */
if (pending) {
    JS_SetPendingException(cx, exception);
}

See Also

Document Tags and Contributors

Contributors to this page: arai, fscholz, gal, Jorend, MMondor, Nickolay, Sgbeal
Last updated by: arai,
Hide Sidebar