Error In Javascript
Contents |
& Guides Learn the Web Tutorials References Developer Guides Accessibility javascript reference error Game development ...more docs Mozilla Docs Add-ons Firefox WebExtensions
Error In Crm
Developer ToolsFeedback Get Firefox help Get web development help Join the MDN community Report a
Javascript Throw New Error
content problem Report a bug Search Search Languages Català (ca) Deutsch (de) Español (es) Français (fr) Bahasa Indonesia (id) 日本語 (ja) 한국어
Javascript Custom Error
(ko) Polski (pl) Português (do Brasil) (pt-BR) Русский (ru) 中文 (简体) (zh-CN) 正體中文 (繁體) (zh-TW) Add a translation Edit Advanced Advanced History Print this article MDN Web technology For developers JavaScript JavaScript reference Standard built-in objects Error Your Search Results ariyankhan josephmcasey AlexanderFradiani fscholz mondwan SphinxKnight janosch-x davidchase Mingun javascript exception object shawnacscott Sheppy ethertank Nickolay evilpie trevorh teoli davidbourguignon secoif DomenicDenicola dgchurchill timemachine3030 Sevenspade Potappo Sephr Brettz9 Mgjbot Allanbonadio Ptak82 JustinLudwig Error In This Article SyntaxParametersDescriptionError typesPropertiesMethodsError instancesPropertiesStandard propertiesVendor-specific extensionsMicrosoftMozillaMethodsExamplesThrowing a generic errorHandling a specific errorCustom Error TypesSpecificationsBrowser compatibilitySee also The Error constructor creates an error object. Instances of Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions. See below for standard built-in error types. Syntax new Error([message[, fileName[, lineNumber]]]) Parameters message Optional. Human-readable description of the error. fileName Optional. The value for the fileName property on the created Error object. Defaults to the name of the file containing the code that called the Error() constructor. lineNumber Optional. The value for the lineNumber property on the created Error object. Defaults to the line number containing the Error() constructor invocation. Description
Learn Bootstrap Learn Graphics Learn Icons Learn How To JavaScript Learn JavaScript Learn jQuery Learn jQueryMobile Learn AppML Learn javascript error handling AngularJS Learn JSON Learn AJAX Server Side Learn SQL Learn javascript try catch PHP Learn ASP Web Building Web Templates Web Statistics Web Certificates XML Learn XML Learn XML fix javascript error AJAX Learn XML DOM Learn XML DTD Learn XML Schema Learn XSLT Learn XPath Learn XQuery × HTML HTML Tag Reference HTML Event Reference HTML Color https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error 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 XML Http Reference XSLT Reference XML Schema Reference Charsets HTML Character Sets http://www.w3schools.com/js/js_errors.asp 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 Swi
As promised, we're going to take a closer look at the Error object that gets passed into the catch clause to see just what we can extract from it in an http://www.javascriptkit.com/javatutors/trycatch2.shtml event of an error. The Error object in all browsers support the following two properties: name: The name of the error, or more specifically, the name of the constructor function the error belongs http://www.webreference.com/programming/javascript/rg31/ to. message: A description of the error, with this description varying depending on the browser. try{ document.body.filters[0].apply() } catch(e){ alert(e.name + "\n" + e.message) } Demo: Six possible values can be returned error in by the name property, which as mentioned correspond to the names of the error's constructors. They are: Error Name Description EvalError An error in the eval() function has occurred. RangeError Out of range number value has occurred. ReferenceError An illegal reference has occurred. SyntaxError A syntax error within code inside the eval() function has occurred. All other syntax errors are not caught by try/catch/finally, and will error in javascript trigger the default browser error message associated with the error. To catch actual syntax errors, you may use the onerror event. TypeError An error in the expected variable type has occurred. URIError An error when encoding or decoding the URI has occurred (ie: when calling encodeURI()). This level of detail may be useful when you wish to sniff out a specific type of error in your catch clause. In the below, no DIV on the page exists with ID="mydiv". When trying to set its .innerHTML property, a TypeError occurs, since we're trying to assign the .innerHTML property to a null object: try{ document.getElementById("mydiv").innerHTML='Success' //assuming "mydiv" is undefined } catch(e){ if (e.name.toString() == "TypeError"){ //evals to true in this case //do something } } Ok, so maybe it's not that useful most of the time, but you just never know. Throwing your own errors (exceptions) Instead of waiting for one of the 6 types of errors above to occur before control is automatically transferred from the try block to the catch block, you can also explicitly throw your own exceptions to force that to happen on demand. This is great for creating your own de