jQuery.Deferred.exceptionHook() Method

Intermediate
5 min read
Updated: Nov 2026
3 examples
Deferred

Definition

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);
CallWhat it does
exceptionHook = function (error, asyncError)Replace the default hook with your own logger or reporter.
jQuery.Deferred.exceptionHookRead the current hook, for example to call it from your replacement.
exceptionHook = nullDisable 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

ParameterDescription
errorany — 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 ValueDescription
undefinedThe return value is ignored. The hook only observes the error.
Promise still rejectsYour 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.

jQuery
jQuery.Deferred.exceptionHook = function (error, asyncError) {
  console.log("hook: " + error.name + " - " + error.message);
};

$.Deferred()
  .resolve()
  .then(function () {
    throw new TypeError("bad input");
  });
Try It Yourself

How It Works

  1. Override. Assign a function to jQuery.Deferred.exceptionHook.
  2. Throw. The .then() callback throws a TypeError.
  3. Observe. jQuery calls your hook with the thrown error (a moment later, asynchronously).

Example 2 — Keep the Default and Add Reporting

Call the original hook from your own so jQuery's console warning is preserved.

jQuery
var original = jQuery.Deferred.exceptionHook;

jQuery.Deferred.exceptionHook = function (error, asyncError) {
  report("Deferred error: " + error.message);   // your own logging
  if (original) {
    original.apply(this, arguments);            // keep jQuery's console.warn
  }
};

$.Deferred().resolve().then(function () {
  throw new Error("save failed");
});
Try It Yourself

How It Works

  1. Save. Keep a reference to the default hook first.
  2. Report. Send the message to your logger, a UI panel, or an error service.
  3. Delegate. original.apply(this, arguments) still prints the usual console warning.

Example 3 — The Promise Still Rejects

The hook observes the error; it does not stop the rejection that follows.

jQuery
var order = [];

jQuery.Deferred.exceptionHook = function (e) {
  order.push("hook: " + e.message);
};

$.Deferred()
  .resolve()
  .then(function () { throw new Error("boom"); })
  .fail(function (e) { order.push("fail: " + e.message); })
  .always(function () { console.log(order.join("\n")); });
Try It Yourself

How It Works

  1. Throw. The callback throws Error("boom").
  2. Hook first. jQuery calls exceptionHook to report the error.
  3. 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.

Next: jQuery.Deferred.getErrorHook() Method

Learn how to capture an Error that shows where an async .then() was set up.

jQuery.Deferred.getErrorHook() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful