Java Script Error
Contents |
References & Guides Learning web development Tutorials References Developer Guides Accessibility Game development ...more docs Mozilla Docs javascript error message Add-ons Firefox Developer ToolsFeedback Get Firefox help Get web development help
Throw Error Java
Join the MDN community Report a content problem Report a bug Search Search Languages Català
Define Inaccuracy
(ca) Deutsch (de) Español (es) Français (fr) 日本語 (ja) Polski (pl) Português (do Brasil) (pt-BR) Русский (ru) 中文 (简体) (zh-CN) Add a translation Edit Advanced Advanced History Print
Define Miscalculation
this article MDN Web technology For developers JavaScript JavaScript reference Statements and declarations throw Your Search Results fscholz ptepper nacyot GuilhermeReda rxgx Dan-Dascalescu Havvy Ginden Sheppy tregagnon safjanowski Shiningtiti Mgjbot Ptak82 Maian Anonymous Dria throw In This Article SyntaxDescriptionExamplesThrow an objectAnother example of throwing an objectRethrow an exceptionSpecificationsBrowser compatibilitySee also The throw statement throws define mistakenly a user-defined exception. Execution of the current function will stop (the statements after throw won't be executed), and control will be passed to the first catch block in the call stack. If no catch block exists among caller functions, the program will terminate. Syntax throw expression; expression The expression to throw. Description Use the throw statement to throw an exception. When you throw an exception, expression specifies the value of the exception. Each of the following throws an exception: throw "Error2"; // generates an exception with a string value throw 42; // generates an exception with the value 42 throw true; // generates an exception with the value true Also note that the throw statement is affected by automatic semicolon insertion (ASI) as no line terminator between the throw keyword and the expression is allowed. Examples Throw an object You can specify an object when you throw an exception. You can then reference the object's properties in the catch bloc
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 javascript error message popup Web Statistics Web Certificates XML Learn XML Learn XML AJAX Learn XML DOM fix javascript error Learn XML DTD Learn XML Schema Learn XSLT Learn XPath Learn XQuery × HTML HTML Tag Reference HTML Event Reference HTML eror 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 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw Mobile Reference AngularJS Reference XML XML Reference XML Http 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 XSLT Examples XPath http://www.w3schools.com/js/js_errors.asp 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 Navigat
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and http://stackoverflow.com/questions/9156176/what-is-the-difference-between-throw-new-error-and-throw-someobject-in-javas policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question 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 script error a minute: Sign up What is the difference between `throw new Error` and `throw someObject` in JavaScript? up vote 120 down vote favorite 28 I want to write a common error handler which will catch custom errors thrown on purpose at any instance of the code. When I did throw new Error('sample') like in the following code try { throw new javascript error message Error({'hehe':'haha'}); // throw new Error('hehe'); } catch(e) { alert(e); console.log(e); } Log shows in Firefox as Error: [object Object] and I couldn't parse the object. For the second throw the log shows as: Error: hehe Whereas when I did try { throw ({'hehe':'haha'}); } catch(e) { alert(e); console.log(e); } the console showed as: Object { hehe="haha"} in which I was able to access the error properties. What is the difference? Is the difference as seen in the code? Like string will be just passed as string and object as objects but the syntax will be different? I haven't explored throwing error object... I had done only throwing strings. Is there any other way than the above two mentioned methods? javascript object error-handling exception-handling throw share|improve this question edited Jun 9 at 17:27 Bergi 218k19232375 asked Feb 6 '12 at 6:07 Jayapal Chandran 2,52984880 2 The problem with throw new Error({prop:val}) is that's not a valid construction of Error. Error has known properties as discussed by Hemant. –grantwparks May 14 '13 at 15:32 related: Throwing strings instead of Errors –Be