[Javascript advanced] exception

Source: Internet
Author: User
Tags try catch

If the keyword Error is used for an exception, we can create an exception instance like a new common object. var err = new Error ("this is an exception"). The attribute of the exception is as follows: description: error description fileName: incorrect file name lineNumber: number of wrong lines message: error message name: Error Type number: Error Code stack: the error Stack information similar to the Stack Trace in Java may be incompatible among browsers. You can understand the basics. Subclass: Error is the parent class of all exceptions. It has many subclasses, such as TypeError, which causes this exception object in case of an unexpected type, such as undeclared variables. When SyntaxError is used to parse js Code, the syntax error in SyntaxError triggers this exception object. This exception object is thrown when ReferenceError uses an invalid reference. EvalError causes this exception object when the eval function is called incorrectly. RangeError causes this exception when the value of a numeric variable exceeds its range. URIError causes this exception object when the encodeURI or decodeURI function is used incorrectly. And so on. Capturing try catch is actually the most important issue for us. We will not be able to identify a new exception by ourselves. Isn't it okay? We understand exceptions to better handle them. Like other back-end languages, javascript uses try {} catch () {} to catch exceptions. try {// The Code section where exceptions may occur} catch (e) {// handle exceptions} finally {// execute at any time} get an actual example try {unde // undefined variable} catch (e) {console. dir (e) ;}finally {alert ("finally") ;}this is an error that captures the Code itself. We can think that some exceptions are thrown, this is the role of the throw keyword. Let's look at two examples: try {throw new Error ("throwing an exception"); // throwing an exception} catch (e) {alert (e. message); // catch to exception} Then try {throw ("throw an exception"); // throw a string} catch (e) {alert (e ); // catch to this string} throw can throw any object, in the catch. Onerror another exception for capturing javascript is window. onerror = function () {} refers to listening to abnormal events like common events. It has some parameters that may be different from each browser. Let's look at the specific usage window. onerror = function (message, URI, lineNumber, columnNumber, errobj) {console. log ("error message:", nessage); console. log ("error file:", URI); console. log ("error row number:", lineNumber); console. log ("error column number:", columnNumber); console. dir (errobj);} throw new Error ("My exceptions"); the result is as follows: although exceptions are caught, the browser displays the Error information by default. In window. onerror = function () {} adds a return value return true, so that the default error information is not displayed, so that window. onerror = function (message, URI, lineNumber, columnNumber, errobj) {console. log ("error message:", nessage); console. log ("error file:", URI); console. log ("error row number:", lineNumber); console. log ("error column number:", columnNumber); console. dir (errobj); return true;} throw new Error ("My exception'); this will not display the default browser Error information. With this, we can ignore all the errors, and we can play a prank, just like window. onerror = function () {return true;} In fact, if we do not repeat the onerror method, it turns out to be empty. If we do not write it, we will not get some exception information. For exceptions in Asynchronization, first let's take a look at the simple asynchronous functions. The simplest is that setTimeout has setTimeout (function () {console. log (1)}, 1000); That's all, simple, lie to you, it will be asynchronous later. First of all, asynchronous is not the main character of today, let's throw an exception in the asynchronous function. Try setTimeout (function () {throw new Error ("My exception")}, 1000). Now we can capture the exception, first try catch try {setTimeout (function () {throw new Error ("My exceptions")}, 1000);} catch (e) {alert (e. message);} this way, the exception cannot be caught, because function () {throw new Error ("My exception") is not in the memory stack when trycatch is executed, this is a bit impressive. It will be detailed in asynchronous mode. Remember that errors cannot be captured. Of course, you can capture setTimeout (function () {try {throw new Error ("My exception")} catch (e) {alert (e. message) ;}}, 1000); we can use window. onerror method to capture asynchronous exceptions, such as window. onerror = function (message, URI, lineNumber, columnNumber, errobj) {console. log ("error message:", message); console. log ("error file:", URI); console. log ("error row number:", lineNumber); console. log ("error column number:", columnNumber); console. dir (errorObj); return true;} setTimeout (fun Ction () {throw new Error ("My exceptions")}, 1000); this is certainly acceptable, because it is a listener method and when an Error occurs. Summary

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.