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!
};
Call
What it does
jQuery
Learn 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
Parameter
Description
Step 1
Detect an invalid state in a plugin (missing option, bad type, etc.).
Step 2
Call jQuery.error("clear message") — execution stops there unless caught.
Step 3
Optionally override jQuery.error once for app-wide logging.
Return Value
Return Value
Description
Value / undefined
Getters 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);
}
Guard. Check required options before touching the DOM.
Throw.jQuery.error stops bad calls early.
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.