jQuery .error() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Plugin utility

What You’ll Learn

jQuery.error(message) throws an exception from a string. This tutorial covers the default throw behavior, the official override pattern (log then throw), try/catch, plugin validation, comparison with throw new Error(), five examples, and try-it labs.

01

Throw

From string

02

Override

For logging

03

Still throw

Keep semantics

04

try/catch

Handle it

05

Plugins

Validation

06

Since 1.4.1

Utility

Introduction

Plugin authors often need a single place to report “this option is invalid” or “this method was called wrong.” Since jQuery 1.4.1, jQuery.error(message) is that hook: it takes a string and throws.

The real power is overrideability. Apps can replace jQuery.error to send messages to a logging service, then still throw so callers and the debugger behave the same.

For everyday app code, throw new Error(message) is clearer. Learn jQuery.error so you can read plugins, wire reporting, and avoid silent overrides. See the Utilities hub for related helpers, and jQuery.readyException for ready-callback errors (a different API).

Understanding the jQuery.error() Method

By default, jQuery.error(message) throws an exception whose content comes from message. Execution stops at that line unless a try/catch handles it.

Overriding is intentional: assign jQuery.error = function(message) { … }. The official docs show logging first, then throw new Error(message). Skipping the throw changes semantics and can hide bugs.

💡
Beginner Tip

Think of jQuery.error as a replaceable throw helper for the jQuery ecosystem — not as Ajax error: callbacks or .error() event binding (removed/legacy).

📝 Syntax

Signature (jQuery 1.4.1+):

jQuery
jQuery.error( message )

Parameters

  • message (String) — the text included in the thrown exception.

Return value

  • Does not return — it throws. There is no normal completion path.

Official override pattern

jQuery
jQuery.error = function( message ) {
  sendErrorLog( "jQuery error: " + message );
  throw new Error( message );
};

⚡ Quick Reference

GoalCode
Throw with a messagejQuery.error( "Invalid option" );
Catch ittry { jQuery.error("x"); } catch (e) { … }
Override + logLog, then throw new Error(message)
App-level throwPrefer throw new Error(message)
Not for AjaxUse .fail() / error callbacks instead
Default
throw

Turns the string into an exception

Override
hook

Replace jQuery.error for logging/UI

Must
rethrow

Keep throwing after custom handling

Audience
plugins

Shared error path for the jQuery stack

📋 jQuery.error() vs throw new Error() vs Ajax errors

jQuery.error(msg)throw new Error(msg)Ajax error / .fail()
PurposeOverridable throw helperNative exceptionHTTP / transport failures
Override hookYes (jQuery.error = …)NoCallbacks / events
Best forPlugins / shared reportingApp logicNetwork requests

Examples Gallery

Examples follow the official jQuery API and common plugin patterns. Use View Output or Try It Yourself to explore each case.

📚 Getting Started

Throw and override — the two core uses.

Example 1 — Throw a Message

Call jQuery.error with a string; an exception is raised.

jQuery
jQuery.error( "Something went wrong in the plugin" );
Try It Yourself

How It Works

The default implementation turns your string into a thrown exception. Without try/catch, the browser reports it in the console and stops that call stack.

Example 2 — Official Pattern: Override for Logging

Replace jQuery.error, send the message to a logger, then throw.

jQuery
jQuery.error = function( message ) {
  // sendErrorLog( "jQuery error: " + message );
  console.error( "jQuery error:", message );
  throw new Error( message );
};

jQuery.error( "Logged and rethrown" );
Try It Yourself

How It Works

Any later jQuery.error(...) call (from your code or a plugin) hits your function. Logging is a side effect; throwing preserves the original “this is fatal” contract.

📈 Practical Patterns

Catching, validating plugin options, and comparing with native throw.

Example 3 — Catch With try/catch

Handle the exception so the rest of the page can continue.

jQuery
try {
  jQuery.error( "Recoverable plugin issue" );
} catch ( err ) {
  $( "#out" ).text( "Caught: " + err.message );
}
Try It Yourself

How It Works

Like any throw, jQuery.error can be caught. Use this in demos or when a host page must isolate a plugin failure without blanking the UI.

Example 4 — Plugin Option Validation

A tiny plugin rejects missing required options via jQuery.error.

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

// Throws — missing color
// $( "p" ).spotlight( {} );

// OK
$( "p" ).spotlight( { color: "#fde68a" } );
Try It Yourself

How It Works

Failing fast with a clear message helps consumers fix their call site. Using jQuery.error lets a host page override reporting once for every plugin that uses the hook.

Example 5 — jQuery.error vs throw new Error

Same visible result by default; the difference is the override hook.

jQuery
// Native — not routed through jQuery.error
// throw new Error( "native" );

// Hookable — plugins/apps can override jQuery.error
jQuery.error( "via jQuery.error" );
Try It Yourself

How It Works

Choose based on whether you need a single replaceable entry point. Application modules rarely need that; jQuery plugins sometimes do.

🚀 Common Use Cases

  • Plugin argument checks — reject missing selectors, options, or types with a clear message.
  • Central logging — override once to ship every jQuery.error call to your monitor.
  • Internal assertions — jQuery and plugins can share one throw helper.
  • Dev-only guards — fail loudly when APIs are misused during development.
  • Reading legacy plugins — recognize $.error("…") as a deliberate throw.
  • Not for network errors — use Ajax callbacks / deferreds for HTTP failures.

🧠 How jQuery.error Fits

1

Something is invalid

Plugin code detects a bad state or missing option.

Detect
2

Call jQuery.error(msg)

Default or overridden implementation runs.

Report
3

Optional logging

Custom overrides send the message to a service or UI.

Log
4

Throw (required)

Exception surfaces to catch blocks or DevTools.

📝 Notes

  • Added in jQuery 1.4.1; still present in modern jQuery 3.x.
  • Primary audience: plugin developers, not everyday app scripts.
  • If you override, still throw at the end (official requirement).
  • Not related to Ajax error options or the old .error() event shorthand.
  • Different from jQuery.readyException (ready-callback sync errors).
  • Prefer throw new Error() in application modules unless you need the hook.
  • Prefix plugin messages with the plugin name for searchable logs.

Browser Support

jQuery.error() has been part of jQuery since 1.4.1+. It works in every browser supported by your jQuery build — including modern evergreen browsers with jQuery 3.x and 4.x. Throwing exceptions is standard JavaScript behavior.

jQuery 1.4.1+

jQuery.error()

Stable overridable throw helper across supported jQuery versions. No browser polyfills required.

100% With jQuery 1.4.1+
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
jQuery.error 1.4.1+

Bottom line: Use for plugin hooks; prefer native throw in app code.

Conclusion

jQuery.error() is a small, purposeful API: throw from a string, and optionally override the function for better reporting. Keep the throw so failures stay visible.

Next up: jQuery.sub() (legacy private jQuery copies).

💡 Best Practices

✅ Do

  • Override early in bootstrap if you need centralized logging
  • Always rethrow after custom handling
  • Write clear, plugin-prefixed messages
  • Use try/catch only when you can recover meaningfully
  • Prefer throw new Error() in non-plugin app code

❌ Don’t

  • Swallow errors with an empty override
  • Confuse this with Ajax error callbacks
  • Use it for routine control flow (prefer returns / validation UI)
  • Forget that callers may catch and continue
  • Log sensitive data in overridden messages

Key Takeaways

Knowledge Unlocked

Five things to remember about jQuery.error

The overridable throw helper for plugins.

5
Core concepts
02

Override

Allowed

Hook
03

Still

Throw

Semantics
04

Not

Ajax

Separate
1.4 05

Since

1.4.1

API

❓ 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 no — prefer throw new Error(message) for clarity. Use jQuery.error when you want one hook that plugins and jQuery internals can share, or when you intentionally override jQuery.error for centralized logging.
Assign a new function that does your logging (or 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. There is no useful return value for callers; control jumps to the nearest catch or to the browser’s uncaught-error path.
Yes. wrap the call: try { jQuery.error("boom"); } catch (e) { console.log(e.message); }. The caught object is typically an Error whose message is your string.
No. Ajax failures use .fail(), error callbacks, and ajaxError events. jQuery.error is a small throw helper for plugins and internal assertions — a different API.
Did you know?

jQuery.error is deliberately tiny so hosts can replace it without forking plugins. The contract is cultural as much as technical: report the problem, then throw. That pattern shows up in many jQuery-era libraries that expected a shared failure channel on the jQuery object.

Next: jQuery.sub() Method

Legacy API for creating a private copy of jQuery (removed in 1.9).

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.

6 people found this page helpful