[JavaScript] Error Handling

Source: Internet
Author: User
1. try-catchECMA-262 version 3rd introduces the try-catch statement, the basic syntax is as follows: try {} catch (error) {} if any code in the try block has an error, the catch Block is executed immediately. In this case, the catch block receives an object containing the error message. Even if you do not use... syntaxHighl 1. try-catchECMA-262 version 3rd introduces the try-catch statement, the basic syntax is as follows: try {} catch (error) {} if any code in the try block has an error, the catch Block is executed immediately. In this case, the catch block receives an object containing the error message. Even if you do not use this error object, you need to name it. The actual information contained in the object varies with the browser, but it is a message attribute that stores the error message, the ECMA-262 also specifies a name property that saves the error type, which is supported by all browsers (except for versions earlier than Opera 9 ). 2. although finally is optional in a try-catch Block, once a finally clause is used, it will be executed in any way, and even the return statement will not be blocked, for example: function test () {try {return 1 ;}catch (error) {return 2 ;}finally {return 3 ;}} 3. error Type each Error has a corresponding Error type, and when an Error occurs, it throws the corresponding type of Error object, the ECMA-262 defines 7 Error types: 1) Error: base type. 2) EvalError: thrown when an exception occurs using the eval () function. 3) RangeError: Throw when the value exceeds the corresponding range. 4) ReferenceError: thrown when the object cannot be found. 5) SyntaxError: A syntax error occurs when the string in the eval () function is used. 6) TypeError: thrown when the unexpected type is saved in the variable or the method that does not exist is accessed. 7) URIError: If the URI format in encodeURI or decodeURI () is incorrect, it is thrown. 4. The throw operator matches the try-catch operator and is used to throw custom errors. When an error is thrown, you must specify a value for the throw operator. The type of this value is not required, for example, throw 123; throw "Hello World! "; When the throw operator is encountered, the code will immediately stop execution. 5. errors that are not handled through try-catch will trigger the error event of the window object. In any browser, the onerror event handler does not create an event object, but it can accept three parameters: the error message, the URL of the error, and the row number. If an error occurs, whether it is generated by the browser or not, the error event is triggered and the event handler is executed. If false is returned in the event handler, the browser can be prevented from reporting the default behavior of the error, example: window. onerror = function (message, url, line) {alert (message); return false;} the image also supports error events, an error event is triggered as long as the URL in the src attribute of the image cannot return a recognizable image format.
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.