jQuery API

jQuery.error

jQuery.error( message )

Description: Takes a string and throws an exception containing it.

  • version added: 1.4.1jQuery.error( message )

    messageThe message to send out.

This method exists primarily for plugin developers who wish to override it and provide a better display (or more information) for the error messages.

Example:

Override jQuery.error for display in Firebug.

jQuery.error = console.error;

Support and Contributions

Need help with jQuery.error or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to jQuery.error? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • because more than the single throw can happen inside the try block... having the catch, allows you to use the same handler for multiple error possibilities... I use this a lot for ajax callbacks, as my server-side code will return '{"error": { "message": "...", "debug":"..." } }' for example when it catches an error in code, but if the server itself throws an error, it won't be something the json parser can parse.. in either state (or others) I can't utilize the data, and finish out the request callback. So I raise an error that gets handled in the catch block. That's the whole point of a try/catch block is to throw errors to a single point of handling, and be able to handle them.
  • I generally stub out the console (based on firebug) for browsers without one. I also tend to throw an error within try/catch blocks. I really don't see an inherit advantage here.

    try {
    var tmp = doSomething();
    if (tmp == something.errorCondition)
    throw new Error("Error condition in X");
    } catch(err) {
    //handle ((err && err.message) || err.toString())
    }
  • Throwing an exception within a try block will just catch the exception in the catch-block - whats the point?
  • Suresh
    I can use jquery.error like this
    <script type="text/javascript">
    jQuery(function(){jQuery.error = console.error;
    console.log("My test message");
    jQuery.error("mytest error message");

    });
    </script>

    Is there any other ways or advantages of this jQuery.error usage?