Summary of JavaScript Exception Handling

Source: Internet
Author: User

In the previous articleArticleWe have summarized the 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:

Code

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 actualCode, We may write this:

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); // comment out this line 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 in the

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:

Code

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:

Code

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.

finally, I want to talk a few more things. During the weekend break last week, I reviewed a Javascript script file that I wrote a long time ago. With hundreds of lines of code, I thought I was writing very well, but I didn't even see any handling of exceptions. I felt very sad and had a deep sense of guilt (the child shoes who maintained this part of the code, when you go through debugging in a dazzling mix of English words (or their short form) or a mess of Pinyin (or their short form), Lou pig sincerely wishes you, please take care of your health ). As a result, the old technology is very powerful, and "no one is born to be perfect" is used to comfort a small heart that is a little hurt, but it still cannot make yourself feel at ease. Lou pig asked another it man who lives with Lou pig, while watching a movie, he said that he never knew the client could write try... catch cloud ...... in this case, there seems to be an excited voice in the head of the NC tower pig: the savior, the savior, very good, very good, it is really good, finally, I had a slight balance in my mind (accidentally exposing the pig's heart too dark, and it was also difficult to face the wall ).
BTW: Now, I think back to his tone of answering the question. Well, it's careless. It's not like fooling Lou pig. I accidentally "deliberately" comfort Lou pig, right?
today is the fool's day.

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.