jQuery .error() Method

Beginner
5 min read
Updated: Oct 2026
4 examples
Live preview

What Is jQuery.error()?

jQuery.error(message) (alias $.error) takes a string and throws an exception containing it (added in jQuery 1.4.1). Plugin authors can override the function for logging or richer display.

Remember
jQuery.error(message)  → throws (no normal return)

try {
  $.error("Missing option");
} catch (err) {
  console.log(err.message);  // "Missing option"
}

// Official override pattern:
jQuery.error = function (message) {
  sendErrorLog("jQuery error: " + message);
  throw new Error(message);   // still throw!
};
NeedUse
Shared plugin throw hook$.error(msg)
Plain app codethrow new Error(msg)
Ajax failuresajaxError / .fail()

How to Use jQuery.error()

Pass a message string; catch it if the caller should recover.

  1. Detect an invalid state in a plugin (missing option, bad type, etc.).
  2. Call jQuery.error("clear message") — execution stops there unless caught.
  3. Optionally override jQuery.error once for app-wide logging.
  4. In the override, log first, then throw new Error(message).
PieceRequired?Role
messageYesString included in the thrown error
Return—Does not return — throws
OverrideOptionalLog / display, then throw again

Pseudocode

Pseudocode
function error(message):
    throw exception with message

// Plugin check:
if options.color is missing:
    jQuery.error("spotlight: options.color is required")

// Override (official idea):
jQuery.error = function (message):
    sendErrorLog("jQuery error: " + message)
    throw new Error(message)

Cheat Sheet

Keep these facts handy while throwing plugin errors with jQuery.

TopicDetail
SyntaxjQuery.error(message) / $.error(message)
SincejQuery 1.4.1
InputString message
BehaviorThrows — no normal return
Main useOverridable hook for plugins
Override ruleLog, then still throw
Not for AjaxUse ajax error APIs

Live Preview

Call $.error (caught for the demo) or run the official override pattern.

Demo catches the throw so the page stays usable.

Result
Pick a mode and click Run.

jQuery Examples

Short, copy-ready snippets. Demos catch throws so the editor stays stable.

Example 1 — Throw and Catch a Message

Call jQuery.error and read err.message in a catch.

jQuery
try {
  jQuery.error("Something went wrong in the plugin");
} catch (err) {
  console.log("Caught: " + err.message);
}
Try It Yourself

How It Works

  1. Call. jQuery.error throws with your string.
  2. Catch. try/catch stops the exception from becoming uncaught.
  3. Message. err.message is the string you passed.
  4. Continue. Code after the catch can still run.

Example 2 — Official Override for Logging

Log the message, then throw new Error(message).

jQuery
jQuery.error = function (message) {
  console.log("jQuery error: " + message);  // stand-in for sendErrorLog
  throw new Error(message);
};

try {
  jQuery.error("Logged and rethrown");
} catch (err) {
  console.log("caught: " + err.message);
}
Try It Yourself

How It Works

  1. Replace. Assign a new function to jQuery.error.
  2. Log. Record the message (service, console, UI).
  3. Throw. throw new Error(message) keeps the API contract.
  4. Catch. Callers still handle failures the same way.

Example 3 — Plugin Option Validation

Fail fast when a required option is missing.

jQuery
$.fn.spotlight = function (options) {
  if (!options || !options.color) {
    jQuery.error("spotlight: options.color is required");
  }
  return this.css("color", options.color);
};

try {
  $("#t").spotlight({});
} catch (err) {
  console.log(err.message);
}
Try It Yourself

How It Works

  1. Guard. Check required options before touching the DOM.
  2. Throw. jQuery.error stops bad calls early.
  3. Message. Include the plugin name for easier debugging.
  4. Happy path. Valid options continue into normal plugin logic.

Example 4 — jQuery.error vs throw new Error

Both throw; only jQuery.error is a single overridable hook.

jQuery
try {
  throw new Error("native");
} catch (err) {
  console.log(err.message);  // "native"
}

try {
  jQuery.error("via jQuery.error");
} catch (err) {
  console.log(err.message);  // "via jQuery.error"
}
Try It Yourself

How It Works

  1. Native. throw new Error is clear and needs no jQuery.
  2. Hook. jQuery.error can be replaced once for all plugins.
  3. Catch. Both land in try/catch the same way.
  4. Choose. Apps → native throw; shared plugin hook → $.error.

Edge Cases & Pitfalls

Check these before overriding or calling jQuery.error.

override

Always throw after logging

An override that only logs swallows failures. End with throw new Error(message).

ajax

Not an Ajax error API

Network / HTTP failures belong to ajaxError and .fail() — not $.error.

uncaught

Uncaught throws stop scripts

Without try/catch, the exception surfaces as an uncaught error. Catch when recovery is possible.

clarity

Prefer throw new Error in apps

Reserve $.error for the shared plugin hook. Everyday app code is clearer with a native throw.

Browser Support

jQuery.error() is available since jQuery 1.4.1+ in all modern browsers with jQuery loaded. It is a thin throw helper — not a browser API.

✓ jQuery 1.4.1+ · Stable

jQuery.error()

Works in Chrome, Firefox, Safari, and Edge whenever jQuery 1.4.1+ is loaded. Prefer throw new Error() in application code unless you need an overridable plugin hook.

100% With jQuery loaded
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
$.error() jQuery 1.4.1+

Bottom line: Use $.error(msg) as a shared plugin throw hook. Override only if you still throw afterward.

Performance Notes

ApproachCostBest for
$.error(msg)Throw pathRare validation failures
throw new Error(msg)Throw pathApp code (clearer)
Logging overrideI/O boundKeep logging cheap; never block UI loops

Throwing is for exceptional cases, not normal control flow. Do not call $.error inside hot loops — validate early and cheaply.

Key Takeaways

  • Role: $.error(msg) throws with that message.
  • Override: log, then still throw new Error(msg).
  • Audience: plugin authors more than everyday app code.
  • Not Ajax: use ajax error APIs for HTTP failures.

One line: $.error is a throw helper you can override for logging — but you must still throw.

Frequently Asked Questions

jQuery.error(message) takes a string and throws an exception containing that message. It exists mainly so plugin authors can override it for custom reporting while still throwing. Available since jQuery 1.4.1.
Usually prefer throw new Error(message) for clarity. Use jQuery.error when you want one shared hook that plugins can override for centralized logging.
Assign a new function that logs (or updates UI), then still throw — typically throw new Error(message). The official docs warn: if you override, remember to throw at the end to preserve semantics.
It does not return normally — it throws. Control jumps to the nearest catch or to the browser’s uncaught-error path.
Yes. try { jQuery.error("boom"); } catch (e) { console.log(e.message); }. The caught value is typically an Error whose message is your string.
No. Ajax failures use .fail(), error callbacks, and ajaxError. jQuery.error is a small throw helper for plugins — a different API.

Next: jQuery.sub()

Learn how jQuery created an isolated copy of itself (legacy API).

jQuery.sub tutorial →

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