Javascript programming Exception Handling example _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces how to handle exceptions in javascript programming, and summarizes the skills related to Exception Handling in JavaScript programming based on the analysis of examples, which has some reference value, for more information about how to handle javascript programming exceptions, see the examples in this article. We will share this with you for your reference. The details are as follows:

In the previous article "Summary of common methods of capturing exceptions in asp.net development", we summarized Exception Handling on the asp.net server. Next, I will briefly summarize and discuss the Exception Handling of javascript on the client. In this way, we have a preliminary understanding of asp.net server and client exception handling.

1. Annoying Script Error

Lou pig often installs 13, but generally has no depth. After learning a piece of English by chance, I can try again:

When browsing Web pages on the internet, we all have seen a JavaScript alert box telling us there is a runtime error and asking "Do you wish to debug? ". Error message like this may be useful for developers but not for users. When users see errors, they often leave the Web page.

The above paragraph, hum, can't you understand it? An elegant and crude understanding of the nc building is that when we open a webpage, we encounter a webpage pop-up error and ask "Do You Want To debug ?" This sb problem. It is annoying. Normal users often choose the upper right side of the Red Cross, but such prompts may be useful to developers. From this we can see, I am kao, the developer is not normal ?! It seems that Lou pig has incorrect understanding. In fact, it is easy to see that the original article tells us that the ultimate intention should be that the script errors on the Web page are terrible, and the user experience is poor, and a batch of potential users are "scared away" in vain.

2. How to Handle script errors

In js, we usually catch and handle exceptions through try... catch.

try{ //Run some code here}catch(e){ //Handle errors here}

In actual code, we may write the following code:

Function test () {var txt = ""; try {alert (aaa); // aaa is an undeclared variable} catch (e) {txt = "There was an error on this page. \ n "; txt + =" Error message: "+ e. message + "\ n"; txt + = "Error description:" + e. description + "\ n"; txt + = "Error name:" + e. name + "\ n"; // alert (txt); // This line may need to be commented out on the official platform }}

Another common method is to register a common processing method for the onerror event of the window object and place the following code inSection:

window.onerror=function(){  return true;}

The advantage of this method is that if you write it once on the page, no annoying script errors will pop up, which means global processing. For developers, this method may hide potential script errors without being detected. Therefore, you need to comment out the above functions during testing.

3. Error in javascript

(1) common attributes of Error objects

When we capture exceptions, we usually throw an Error object instance e in the catch. Several common attributes of e are as follows:

Attribute description
Description exception description
Message exception description
Name exception type
Unique Exception Code of number

In actual development, a message and name information is usually prompted to developers to handle exceptions in a targeted manner.

(2) Type of the Error object

We can see the exception type through the name attribute in (1. In js, there are several common exception types:

TypeError: This is triggered when an unexpected type is encountered, such as undeclared variables;
SyntaxError: A syntax error occurs when parsing js Code. For example, when the server registers, the error may be caused by missing parentheses or quotation marks;
ReferenceError: this exception is thrown when an invalid reference is used;
EvalError: triggered when the eval function is called incorrectly;
RangeError: triggered when the value of a numeric variable exceeds its range;
URIError: triggered when the encodeURI () or decodeURI () function is incorrectly used.

In actual development, different exception handling methods are made for different types of exceptions, which helps us to effectively discover problems and improve user experience.

I hope this article will help you design JavaScript programs.

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.