Custom Error Object Javascript
Contents |
HTML CSS JavaScript Graphics HTTP APIs / DOM Apps MathML References create custom object javascript & Guides Learn the Web Tutorials References Developer Guides javascript custom error stack trace Accessibility Game development ...more docs Mozilla Docs Add-ons Firefox WebExtensions Developer ToolsFeedback Get Firefox help javascript custom error message Get web development help Join the MDN community Report a content problem Report a bug Search Search Languages Català (ca) Deutsch (de) Español
Javascript Custom Error Handler
(es) Français (fr) 日本語 (ja) Polski (pl) Русский (ru) 中文 (简体) (zh-CN) Add a translation Edit Advanced Advanced History Print this article MDN Web technology For developers JavaScript JavaScript reference Standard built-in objects Error Error.prototype.message Your Search Results fscholz Mingun Sheppy evilpie Sevenspade Waldo Error.prototype.message In This Article js error object DescriptionExamplesThrowing a custom errorSpecificationsBrowser compatibilitySee also The message property is a human-readable description of the error. Description This property contains a brief description of the error if one is available or has been set. SpiderMonkey makes extensive use of the message property for exceptions. The message property combined with the name property is used by the Error.prototype.toString() method to create a string representation of the Error. By default, the message property is an empty string, but this behavior can be overridden for an instance by specifying a message as the first argument to the Error constructor. Examples Throwing a custom error var e = new Error('Could not parse input'); // e.message is 'Could not parse input' throw e; Specifications Specification Status Comment ECMAScript 1st Edition (ECMA-262) Standard Initial definition. ECMAScript 5.1 (ECMA-262)The definition of 'Error.prototype.message' in that specification. Standard ECMAScript 20
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of node js error object this site About Us Learn more about Stack Overflow the company Business Learn
Node Js Error Object Documentation
more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
Object Syntax Javascript
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message How do I create a custom Error in JavaScript? up vote 131 down vote favorite 56 For some reason it looks like constructor delegation doesn't work in the following snippet: function NotImplementedError() { Error.apply(this, arguments); } NotImplementedError.prototype = new Error(); var nie = new NotImplementedError("some message"); console.log("The message is: '"+nie.message+"'") Running this gives The message is: ''. Any ideas as to why, or if http://stackoverflow.com/questions/783818/how-do-i-create-a-custom-error-in-javascript there is a better way to create a new Error subclass? Is there a problem with applying to the native Error constructor that I don't know about? javascript exception share|improve this question edited Jul 26 '13 at 21:01 B T 14.7k1397123 asked Apr 23 '09 at 22:35 cdleary 27.3k40129178 Does nie instanceof NotImplementedError assertion work after your changes? I thought that in order for this to work you need to define NotImplementedError.prototype.constructor explicitly. –jayarjo Jul 9 '11 at 9:45 Next time, please tear out all the extraneous code that isn't required to demonstrate your issue. Also, wtc is js.jar ? Is that needed to reproduce the problem? –B T Jul 26 '13 at 20:39 1 Edited this question so that its understandable in 10 seconds rather than 10 minutes –B T Jul 26 '13 at 20:46 I created an inheritance/class library that inherits from Error types properly: github.com/fresheneesz/proto –B T Sep 10 '13 at 17:33 1 jsfiddle for a few of the top answers. –Nate Sep 22 '15 at 13:56 | show 1 more comment 16 Answers 16 active o
I got a lot of responses and follow-up questions, so I thought I'd continue the discussion by explaining a little more about how to throw your own errors. To review, the basic syntax is: throw new Error("message"); This works in all https://www.nczonline.net/blog/2009/03/10/the-art-of-throwing-javascript-errors-part-2/ browsers and will display the error information in the same way it would any unintentional JavaScript error. The "message" string is stored in the message property of the object (which is what the browser uses to display the message). Periodically, I http://www.w3schools.com/js/js_errors.asp see people throwing errors by just providing the string, such as this: throw "message"; Doing so will cause an error to be thrown, but not all browsers respond the way you'd expect. Firefox, Opera, and Chrome each display an "uncaught custom error exception" message and then include the message string. Safari and Internet Explorer simply throw an "uncaught exception" error and don't provide the message string at all. Clearly, this is suboptimal from a debugging point of view. Of course, you can throw any type of data that you'd like. There are no rules prohibiting specific data types: throw { name: "Nicholas" }; throw true; throw 12345; throw new Date(); The only thing to remember is that throwing any value will result in an javascript custom error error if it's not caught via a try-catch statement. Firefox, Opera, and Chrome all call String() on the value that was thrown to display something logical as the error message; Safari and Internet Explorer do not. The only surefire way to have all browsers display your custom error message is to use an Error object. ECMA-262, 3rd Edition actually specifies seven error object types. These are used by the JavaScript engine when various error conditions occur and can also be manually created: Error - base type for all errors. Never actually thrown by the engine. EvalError - thrown when an error occurs during execution of code via eval() RangeError - thrown when a number is outside the bounds of its range. For example, trying to create an array with -20 items (new Array(-20)). These occur rarely during normal execution. ReferenceError - thrown when an object is expected but not available, for instance, trying to call a method on a null reference. SyntaxError - thrown when the code passed into eval() has a syntax error. TypeError - thrown when a variable is of an unexpected type. For example, new 10 or "prop" in true. URIError - thrown when an incorrectly formatted URI string is passed into encodeURI, encodeURIComponent, decodeURI, or decodeURIComponent. Understanding that there are different types of errors can make it easier to handle them. All error types inherit from Error so checking the type with instanceof Error doesn't give you any useful inf
Learn Bootstrap Learn Graphics Learn Icons Learn How To JavaScript Learn JavaScript Learn jQuery Learn jQueryMobile Learn AppML Learn AngularJS Learn JSON Learn AJAX Server Side Learn SQL Learn PHP Learn ASP Web Building Web Templates Web Statistics Web Certificates XML Learn XML Learn XSLT Learn XPath Learn XQuery × HTML HTML Tag Reference HTML Event Reference HTML Color Reference HTML Attribute Reference HTML Canvas Reference HTML SVG Reference Google Maps Reference CSS CSS Reference CSS Selector Reference W3.CSS Reference Bootstrap Reference Icon Reference JavaScript JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference AngularJS Reference XML XML Reference XSLT Reference XML Schema Reference Charsets HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8 Server Side PHP Reference SQL Reference ASP Reference × HTML/CSS HTML Examples CSS Examples W3.CSS Examples Bootstrap Examples JavaScript JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples XML XML Examples XSL Examples XSLT Examples XPath Examples XML Schema Examples SVG Examples Server Side PHP Examples ASP Examples Quizzes HTML Quiz CSS Quiz JavaScript Quiz Bootstrap Quiz jQuery Quiz PHP Quiz SQL Quiz XML Quiz × JS Tutorial JS HOME JS Introduction JS Where To JS Output JS Syntax JS Statements JS Comments JS Variables JS Operators JS Arithmetic JS Assignment JS Data Types JS Functions JS Objects JS Scope JS Events JS Strings JS String Methods JS Numbers JS Number Methods JS Math JS Random JS Dates JS Date Formats JS Date Methods JS Arrays JS Array Methods JS Array Sort JS Booleans JS Comparisons JS Conditions JS Switch JS Loop For JS Loop While JS Break JS Type Conversion JS RegExp JS Errors JS Debugging JS Hoisting JS Strict Mode JS Style Guide JS Best Practices JS Mistakes JS Performance JS Reserved Words JS JSON JS Forms JS Forms Forms API JS Objects Object Definitions Object Properties Object Methods Object Prototypes JS Functions Function Definitions Function Parameters Function Invocation Function Closures JS HTML DOM DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM CSS DOM Animations DOM Events DOM EventListener DOM Navigation DOM Nodes DOM Nodelist JS Browser BOM JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies JS JSON JSON Intro JSON Syntax JSON HowTo JSON Http JSON Files JSON SQ