[Transferred] JavaScript exception handling.

Source: Internet
Author: User
Tags try catch

My error handling clause:

Window.onerror = function (errormessage, Scripturi, linenumber, ColumnNumber, errorobj) {console.log ("error message:",    ErrorMessage);    Console.log ("Error file:", Scripturi);    Console.log ("Error Lineno:", linenumber);    Console.log ("Error ColumnNo:", ColumnNumber); Console.log ("Error detail:", errorobj);}




Try Catch finallyStatement DescriptionTry Catchfinally is the exception handling mechanism provided by the JavaScript language. The syntax structure is as followsTry { //This code runs from top to bottom, where any one of the statements throws an exception and the code block ends running}Catch(e) {//If an exception is thrown in a try code block, the code in the catch code block is executed. //e is a local variable that points to an error object or other thrown object}finally { //The finally code block is always executed, regardless of whether the code in the try is thrown abnormally (even if there is a return statement in the try code block). } Try...Catch...finally... The syntax is optional except try catch and finally (both must have one), i.e. try ...Catch...finally..... There are three forms of grammar:Try{//some code}Catch(e) {//Somecode}finally{//some code}Try{//some code}Catch(e) {//Somecode}Try{//some code}finally{//some codeIf there is a catch, once the code in the try throws an exception, it executes the code in the catch and then executes the code in the finally. If there is no catch statement, the code in the try throws an exception, then the statement in the finally is executed, and the exception thrown in the try continues to be thrown up in an unusual manner. Regardless of how execution of a try block is terminated (an exception occurs,return, natural termination) The statements in finally are always executed, precisely because of this feature of finally, which is usually used to perform some cleanup work. If the code in the try is in return,Continue, a break is terminated, and the JavaScript engine executes the return statement in the corresponding try after executing the statement in finally. The throw statement shows that the throw statement has been implemented in javascript1.4. The syntax of a try is simple, as followsThrowexpression, where expression can be of any type, that is, throw "there isa error "or throw 1001 is correct. But usually we throw an error object or subclass of the Error object. About error We'll look at the sample code for a throw before we introduce it later. function factorial (x) {//If The input argument is invalid, throw an exception! if(X <0)Throw NewError ("x must not being negative"); //Otherwise, compute a value and return normally for(varf =1; X >1; F *= x, x--)/*Empty*/ ; returnF;} The Error object and its subclasses are implemented in javascript1.5. There are two types of error constructorsNewError ()Newerror (Message) error has two basic properties of name and message. The message is used to represent the details of the exception. and name refers to the constructor of the error object. In addition, different JS engines provide some extensions to the error, such as that Mozilla provides extensions for filename (the file name of the exception appears) and LineNumber (the line number of the exception), while IE provides support for number (error numbers). However, name and message are two basic properties that can be supported in both Firefox and IE. There are several subcategories of error in JavaScript evalerror, Rangeerror, Referenceerror,syntaxerror, TypeError, Urierror, and their respective meanings are not described here in detail, Readers can find the appropriate reference in the reference document I provide. JavaScript exception handling mechanism and Window.onerror handle when an error occurs in JavaScript code, the JS engine will search for the corresponding catch based on the JS call stack, if no corresponding catch handler or catch is found Handler itself has error or throw a new error, the end will be the processing of this error to the browser, the browser will be in a different way (ie in the yellow triangle pattern displayed in the lower left corner, and Firefix will appear in the error console) display error message to the visitor. In many scenarios, we will find this kind of error prompt is not friendly, and the information is very hidden, then we have the opportunity to customize the way this error prompt? The answer is yes, that is the Window.onerror property. The JavaScript Window object has a special property onerror, if you assign a function to the OnError property of the window, then there is a JavaScript error in this window, The function is called, which means that the function will be the handle to the error handle of the window. //Display Error messages in a dialog box, but never more than 3Window.onerror =function (msg, URL, line) {if(Onerror.num++ <Onerror.max) {alert ("ERROR:"+ msg +"\ n"+ URL +":"+Line ); return true; }}onerror.max=3; Onerror.num=0; The onerror handle will have 3 parameters that are error message prompts, which produce the wrong JavaScript document ULR, and the line number where the error occurred. The return value of the Onerroe handle is also important if the handle returns true, indicating that the browser does not need to do extra processing of the error, which means that the browser does not have to display the error message again. If False is returned, the browser will still prompt for an error message. Window.onerror=function () {alert ("xx");return true;//If you comment out the statement, there will be an error in the browser, or vice versa. }function ThrowError () {Throw NewError ("CC");} 1we can not avoid some JS exception in the process of developing HTML, usually we can not rely on the customer to open the browser error box (such as) to provide us with clues to locate the bug, With the use of window.onerror handle we can tell the error message display, the customer as long as the error occurs, provide the corresponding screenshot, which can be very good to help developers to locate, analysis of JavaScript-related errors. References

[Transferred] JavaScript exception handling.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.