JS_IsExceptionPending

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_IsExceptionPending
  • Revision title: JS_IsExceptionPending
  • Revision id: 93237
  • Created:
  • Creator: gal
  • Is current revision? Yes
  • Comment 106 words added

Revision Content

Determine whether an exception is pending in the JS engine.

Syntax

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

Description

JS_IsExceptionPending returns JS_TRUE if an exception has been thrown in the context cx and the exception has not yet been caught or cleared. Otherwise, it returns JS_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 */

JSBool pending;
jsval exception = JSVAL_VOID;

/* If an exception is pending, save and clear it. */
pending = JS_IsExceptionPending(cx);
if (pending) {
    if (!JS_GetPendingException(cx, &exception))
        return JS_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

{{ LXRSearch("ident", "i", "JS_IsExceptionPending") }}

JS_SetPendingException, JS_GetPendingException, JS_ClearPendingException, JS_ReportPendingException

Revision Source

<p>Determine whether an exception is pending in the JS engine.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="eval"><a href="/En/SpiderMonkey/JSAPI_Reference/JSBool" title="en/JSBool">JSBool</a> <strong>JS_IsExceptionPending</strong>(<a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/JSRuntime">JSContext</a> *cx);
</pre>
<table class="fullwidth-table"> <tbody> <tr> <th>Name</th> <th>Type</th> <th>Description</th> </tr> <tr> <td><code>cx</code></td> <td><code><a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/JSRuntime">JSContext</a> *</code></td> <td>Pointer to a JS context to check for pending exceptions.</td> </tr> </tbody>
</table>
<h2 id="Description" name="Description">Description</h2>
<p><code>JS_IsExceptionPending</code> returns <code>JS_TRUE</code> if an exception has been thrown in the context <code>cx</code> and the exception has not yet been caught or cleared. Otherwise, it returns <code>JS_FALSE</code></p>
<p>This can be used from <code><a href="/en/SpiderMonkey/JSAPI_Reference/JSNative" title="en/JSNative">JSNative</a></code> functions which call JS code to determine if the called JS code threw an exception or not.</p>
<h2 id="Example">Example</h2>
<pre class="eval">/* <strong>JSAPI</strong> */

JSBool pending;
jsval exception = JSVAL_VOID;

/* If an exception is pending, save and clear it. */
pending = JS_IsExceptionPending(cx);
if (pending) {
    if (!JS_GetPendingException(cx, &amp;exception))
        return JS_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, &amp;exception);
}
</pre>
<h2 id="See_Also" name="See_Also">See Also</h2>
<p>{{ LXRSearch("ident", "i", "JS_IsExceptionPending") }}</p>
<p><a href="/en/SpiderMonkey/JSAPI_Reference/JS_SetPendingException" title="en/JS_SetPendingException">JS_SetPendingException</a>, <a href="/en/SpiderMonkey/JSAPI_Reference/JS_GetPendingException" title="en/JS_GetPendingException">JS_GetPendingException</a>, <a href="/en/SpiderMonkey/JSAPI_Reference/JS_ClearPendingException" title="en/JS_ClearPendingException">JS_ClearPendingException</a>, <a href="/en/SpiderMonkey/JSAPI_Reference/JS_ReportPendingException" title="en/JS_ReportPendingException">JS_ReportPendingException</a></p>
Revert to this revision