jQuery .error() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
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!
};
CallWhat it does
jQueryLearn jQuery.error(): throw an exception from a message string, override for logging while still throwing, plugin validation patterns, examples, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Detect an invalid state in a plugin (missing option, bad type, etc.).
Step 2Call jQuery.error("clear message") — execution stops there unless caught.
Step 3Optionally override jQuery.error once for app-wide logging.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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() Method

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

jQuery.sub() 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