The catch() method attaches an error handler to a jQuery Deferred or promise chain. It runs when the operation is rejected or when a then() callback throws — giving you a familiar, Promise-style way to centralize failure handling.
01
Syntax
dfd.catch(fn)
02
On reject
Handles failures
03
vs fail()
Same behavior
04
Chain
then → catch
05
Throws
Catches exceptions
06
jQuery 3+
Promises/A+ API
Fundamentals
Introduction
Async code fails — networks drop, APIs return errors, and validation throws. Without a dedicated error path, failures either crash your script or leave users staring at a broken UI. jQuery’s Deferred API gives you fail() for rejections; since jQuery 3.0, catch() offers the same behavior with Promise-style naming.
Use catch() at the end of a chain (or after any then()) to log errors, show messages, retry requests, or fall back to cached data — all in one readable place instead of scattering checks everywhere.
Concept
Understanding the catch() Method
catch() is part of jQuery’s Promises/A+ compatible Deferred interface (jQuery 3.0+). When you call deferred.catch(errorHandler), jQuery queues your function and invokes it when:
The Deferred is rejected via reject() or rejectWith().
A then() callback throws an exception.
A then() callback returns a rejected promise or Deferred.
The handler receives the rejection reason — often a string, Error object, or AJAX error details. It does not run when the chain resolves successfully.
💡
Beginner Tip
If you know native JavaScript Promise.catch(), jQuery’s version works the same way on Deferred chains. On jQuery 3+, catch() and fail() are interchangeable.
Foundation
📝 Syntax
General form of deferred.catch:
jQuery
deferred.catch( errorHandler [, errorHandler ] )
Parameters
errorHandler — function executed when the Deferred is rejected or when an upstream then() fails. Receives the rejection reason as argument(s). You may register multiple handlers.
Return value
Returns the same Deferred/promise object for chaining further then() or catch() calls.
On success, then() runs and catch() is skipped. Change the call to dfd.reject("404") and only catch() executes — that is the pattern from the original reference example, expanded for clarity.
📈 Practical Patterns
Real-world error paths in multi-step async chains.
Example 3 — Catch Exceptions Inside then()
Throwing in a then() callback converts the failure into a rejection that catch() handles.
jQuery
$.Deferred()
.resolve({ id: 0 })
.then(function (data) {
if (!data.id) {
throw new Error("Invalid user id");
}
console.log("Valid id:", data.id);
})
.catch(function (err) {
console.error("catch:", err.message);
});
Even though the Deferred initially resolved, the thrown Error inside then() propagates down the chain — just like native Promises — and triggers catch().
Example 4 — AJAX-Style Error Message
Simulate a failed request and show a user-friendly message in catch().
With real $.ajax(), you would attach the same catch() to the jqXHR promise. One handler covers network errors, HTTP failures, and parse errors passed through the chain.
Example 5 — Single catch() for a Multi-Step Chain
Place one error handler at the end; rejections from any intermediate step bubble down to it.
Steps one and two succeed; step three rejects. The final then() is skipped and catch() receives the rejection — centralizing error handling for the whole pipeline.
Applications
🚀 Use Cases
Promise chains — attach one catch() after several then() steps to handle any failure in the pipeline.
AJAX requests — show error toasts or fallback UI when $.ajax() rejects.
Chained Deferreds — keep error logic consistent when each step returns a new promise.
Validation errors — throw inside then() and let catch() format the message for users.
Logging — record structured error context in one place before optional recovery.
🧠 How catch() Handles Failures
1
Chain runs
then() callbacks execute on success. catch() waits in the queue.
Pending
2
Failure occurs
A step rejects, throws, or returns a rejected promise — the chain enters failed state.
Reject
3
catch() runs
Registered handlers receive the reason. Downstream then() success handlers are skipped.
Handle
=
🛡
Graceful recovery
Users see helpful feedback instead of silent failures — or the chain recovers if catch returns a value.
Important
📝 Notes
catch() requires jQuery 3.0+. On older versions, use fail().
catch is a reserved word in older ES5 strict contexts — jQuery still exposes it as a method name on objects.
Returning a plain value from catch() can resume the chain with a resolved promise.
Pair with always() when you also need cleanup on success.
For brand-new code without jQuery, native Promise.catch() follows the same mental model.
Compatibility
Browser Support
deferred.catch() is available in jQuery 3.0+ as part of Promises/A+ compatibility. It works wherever that jQuery version runs.
✓ jQuery 3.0+
jQuery Deferred.catch()
Supported in jQuery 3.x across all browsers jQuery supports. Not available in jQuery 1.x or 2.x — use fail() instead.
100%With jQuery 3+
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
deferred.catch()Universal
Bottom line: Load jQuery 3.7+ for modern promise-style chains. Legacy projects on jQuery 2.x should keep using fail().
Wrap Up
🎉 Conclusion
The deferred.catch() method gives you a clean, Promise-style way to handle failures in jQuery async chains. Centralize error messages, logging, and fallbacks in one callback instead of duplicating checks after every step.
Practice the five examples above, then explore jQuery.Deferred() to see how Deferred objects are created and wired into your own APIs.
Forget to propagate errors you cannot recover from
Mix unhandled throws outside the promise chain
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about catch()
Reliable error handling for jQuery async chains.
5
Core concepts
📝01
Syntax
dfd.catch(fn)
API
❌02
Reject only
Not on success
Scope
=03
fail()
Same behavior
Alias
🔗04
Propagates
Chain-wide
Pattern
3+05
jQuery 3
Promises/A+
Version
❓ Frequently Asked Questions
catch() registers a callback that runs when the Deferred is rejected or when an earlier then() callback throws an error or returns a rejected promise. It is the promise-style counterpart to fail().
In jQuery 3.0+, catch() is an alias for fail() on Deferred and Promise objects. They behave the same — catch() exists so jQuery chains look like native Promise code.
No. catch() runs only when the chain is rejected. Successful resolution skips catch() and continues to the next then() handler.
catch() was added in jQuery 3.0 as part of Promises/A+ compatibility. Use fail() if you must support jQuery 1.x or 2.x.
Yes. If your catch handler returns a normal value or resolves a new Deferred, the chain can continue with then(). If catch re-throws or returns a rejected promise, the chain stays rejected.
Use catch() (or fail()) for error-specific logic — messages, retries, fallbacks. Use always() for shared cleanup that runs on both success and failure, like hiding spinners.
Did you know?
jQuery added catch() in version 3.0 so Deferred chains could mirror native Promise syntax — but under the hood it still uses the same rejection machinery as the original fail() method from jQuery 1.5.