Determine whether an exception is pending in the JS engine.


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


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.


/* 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;

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

if (JS_IsExceptionPending(cx)) {

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


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

See Also

Document Tags and Contributors

Contributors to this page: mdnwebdocs-bot, arai, fscholz, gal, Jorend, MMondor, Nickolay, Sgbeal
Last updated by: mdnwebdocs-bot,