jQuery.Deferred.exceptionHook( error, asyncError ) is a function jQuery calls whenever an exception is thrown inside a .then() callback (added in jQuery 3.0). Promises/A+ turns such errors into rejections, so they can disappear silently — this hook lets you see them. By default it logs a warning with the stack to the console. It is an advanced diagnostics tool.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
jQuery.Deferred.exceptionHook = function (error, asyncError) {
// error → the value that was thrown (usually an Error)
// asyncError → optional Error or stack from getErrorHook() / getStackHook()
};
// Default (jQuery 3.x) - warns only for built-in error types:
// console.warn("jQuery.Deferred exception: " + error.message,
// error.stack, asyncError);
Call
What it does
exceptionHook = function (error, asyncError)
Replace the default hook with your own logger or reporter.
jQuery.Deferred.exceptionHook
Read the current hook, for example to call it from your replacement.
exceptionHook = null
Disable the hook — jQuery checks that it exists before calling it.
.then(function () { throw ... })
The situation that triggers the hook.
The hook runs on the same tick as the failure but usually after an async hop (setTimeout), so do not expect it to fire synchronously inside .then().
Parameters
Parameter
Description
error
any — the value that was thrown, normally an Error object with name, message and stack.
asyncError (optional)
The value returned by jQuery.Deferred.getErrorHook() (or getStackHook()), or undefined if neither is defined. Older docs and jQuery 3.0–3.6 call this argument stack.
Return Value
Return Value
Description
undefined
The return value is ignored. The hook only observes the error.
Promise still rejects
Your hook cannot swallow the error: the promise from .then() is still rejected, so .fail() and .catch() handlers run afterwards.
jQuery Examples
Example 1 — Log Every Deferred Exception
Replace the hook to see each error thrown inside a .then() callback.
Hook first. jQuery calls exceptionHook to report the error.
Then reject. The returned promise is rejected, so .fail() runs second.
Frequently Asked Questions
It is a function property jQuery calls when an exception is thrown inside a .then() callback (jQuery 3.0 and later). Because Promises/A+ converts thrown errors into rejections, they could go unnoticed; the hook gives you a place to log or report them.
It calls console.warn with the message "jQuery.Deferred exception: ...", the error stack, and the optional async error. To avoid noise from intentional rejections, the default only warns for built-in error types such as TypeError, ReferenceError, SyntaxError, RangeError, EvalError, InternalError and URIError.
error is whatever was thrown, usually an Error object. asyncError is the value returned by jQuery.Deferred.getErrorHook() (or the older getStackHook()) at the time the callback was queued, so you can see where .then() was set up. It is undefined if no such hook is defined. Older docs call this second argument stack.
No. The hook only observes. After it runs, the promise returned by .then() is rejected with the thrown error, and your .fail() or .catch() handlers still run. Its return value is ignored.
No. Those handlers run through jQuery.Callbacks, and a thrown error there propagates to the code that called resolve() or reject(). The hook is for .then() and .catch() chains. For DOM-ready callbacks, use jQuery.readyException.
It is safe to wrap it to send errors to a monitoring service, as long as you keep the original behavior if you need it. Treat it as a diagnostics tool and keep the replacement small, because it runs for every failing .then() callback on the page.