The example of this article summarizes the method of JavaScript programming exception handling. Share to everyone for your reference, specific as follows:
Foreword: In the previous article "ASP.net Common Catch Unusual way summary", we summarized the ASP.net service end exception processing. This article goes on to briefly summarize and discuss the exception handling of JavaScript in the client. So we have a preliminary understanding of ASP.net server and client exception handling.
1. Annoying scripting errors
Building pigs often pack 13, but generally there is no depth. Occasionally difficult to read a paragraph of English, and finally can be deeply installed again:
When browsing Web pages on the Internet, we are have seen a JavaScript alert box telling us there is a runtime error and a Sking "Do your wish to debug?". The ERROR message is useful for developers but is not for users. When users are errors, they often leave the Web page.
The above paragraph, hum, do not understand it? NC Building Pig Elegant and rough understanding is that, open a Web page, we have occasionally encountered the Web page pop-up footsteps error and asked "Do you want to debug?" "This SB problem. Annoying ah, normal users will often be accustomed to choose the red fork on the right, but this hint of information may be useful to the TMD developers. This shows that I am Kao, the developer is not normal?! It looks as if the building pig understood wrong. In fact, you can see that the original to tell us the ultimate intention is that the Web page in the script error is very deadly, the user experience is not good, in vain, "scare away" a group of potential users.
2. How to handle script errors
In JS, we usually also use Try...catch to catch and handle exceptions.
Try
{
//run some code here
}
catch (e)
{
//handle errors here
}
In the actual code, we might write this:
function test () {
var txt= "";
try{
alert (AAA);//aaa is an undeclared variable
}
catch (e) {
txt= "There was a error on this page.\n\n";
txt+= "Error message:" + e.message + "\ n";
txt+= "Error Description:" + e.description + "\ n";
txt+= "Error Name:" + e.name + "\ n";
alert (TXT);//The official platform may need to comment out the line
}
}
It is also a more common practice to register a common approach to the OnError event for a Window object and place the following code in the
Window.onerror=function () {return
true;
}
The advantage of this approach is that the page is written once, it will not pop-up annoying script errors, a bit of the meaning of global processing. For developers, this type of writing may hide potential scripting errors without being discovered, so you need to comment out the above function when testing.
3. Error in JavaScript
(1), the common properties of the Error object
When we catch an exception, we usually throw an instance of an Error object at a catch. Several common properties of E,e are as follows:
Attribute description
Description Information for description exception
Description of Message exception
Name Exception type
Number unique Exception code
In actual development, the developer message and name information are usually prompted to handle the exception in a targeted manner.
(2), the type of the Error object
We can see the exception type by using the Name property in (1). In JS, there are several common types of exceptions:
TypeError: Thrown when an unexpected type is encountered, such as undeclared variables;
SyntaxError: When parsing JS code, the syntax error is triggered, such as the service side registration footsteps, a few parentheses or quotes, etc.
Referenceerror: This exception is thrown when an invalid reference is used;
Evalerror: Thrown when the eval function is called incorrectly;
Rangeerror: Thrown when the value of a numeric variable is outside its range;
Urierror: Thrown when the encodeURI () or decodeURI () function is incorrectly used.
In the actual development, it is helpful for us to find out the problems and improve the user experience in order to make different exception handling for different types of anomalies.
I hope this article will help you with JavaScript programming.