JavaScript exception handling

Source: Internet
Author: User

http://www.yaosansi.com/post/747.html Exception Handling Overview
In the process of code, errors are unavoidable, in general, the error occurs in two cases: first, the logic or syntax errors inside the program, the second is the operating environment or the user input unpredictable data caused by errors. For the former, it is called error, can be solved by debugging the program, the latter is more known as the exception (exception), as the name implies, is beyond the conventional, not according to the intention of the program design to enter data. There are, of course, many types of exceptions.
Therefore, exceptions are not equivalent to errors, and sometimes exceptions are used to solve some problems. JavaScript can catch an exception and handle it accordingly, thus avoiding the browser's error to the user.

handling Exceptions using try-catch-finally
The structure can be used by the user to handle code that might occur, and if an exception occurs, it is caught and processed by a catch with the following syntax:
try{
The code to execute
}
catch (e) {
Code to handle exceptions
}
finally{
Code that executes whether or not an exception occurs
}
Through exception processing, the program can be avoided to stop running, which has a certain degree of self-healing ability.
In AJAX development, a typical use of exception handling is to create XMLHttpRequest objects, different browsers create it differently, in order to enable the code to run across browsers, you can take advantage of exceptions, one way not, and then another method until the exception is not occurred, for example:
<script language= "JavaScript" type= "Text/javascript" >
<!--
var xmlhttp;
try{
Try to create an XMLHttpRequest object in IE browser mode
Xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
}catch (e) {
try{
Try common XMLHttpRequest objects in a non-ie browser way
Xmlhttp=new XMLHttpRequest ();
}catch (e) {}
}
-
</script>
This way, you can create XMLHttpRequest objects across browsers. Note that even if you do not process within a catch block, the catch identity and its argument e must be written, otherwise a syntax error is generated, and finnally is not required.
throwing an exception with a throw statement
There is an internal exception mechanism in JavaScript that can automatically throw exceptions when it encounters an illegal operation. In the actual development, with the complexity of the program, you need to be able to implement the exception, which can be achieved by the throw statement:
throw value;
Where value is the exception variable to throw, it can be any type in JavaScript. But inside the exception in JavaScript, the exception parameter (E in catch (e)) is an object named error, which can be created by new error (message), and the description of the exception is used as a property message for the Error object. Can be passed in by the constructor, or it can be assigned later. This exception describes the message, which allows the program to get the details of the exception and automatically process it.
The following program calculates the and of two data, and throws an exception if the argument is not a number, with the following code:
<script language= "JavaScript" type= "Text/javascript" >
<!--
Function default requirement parameter is numeric
function sum (A, b) {
A=parseint (a);
B=parseint (b);
Throws an exception object if a or B cannot be converted to a number
if (IsNaN (a) | | IsNaN (b)) {
throw new Error ("Arguments is not numbers");
}
return a+b;
}

try{
Bad call
var s=sum ("C", "D");
}catch (e) {
Show exception Details
alert (e.message);
}
-
</script>
It is wrong to pass the letter as a parameter to the SUM function in the program, so an exception object is thrown inside the function, which is fetched by the catch statement and displays its details using the alert statement.
Note: Creating an Exception object using new Error (message) is only a default habit and is also the implementation of built-in exceptions. This is not required, and it is entirely possible to throw an exception of any data type, such as an integer, as a description of the exception. As long as the exception is thrown in the program and the catch exception can match.

The Error object has some other properties in addition to the message property, which vary by browser, for example: in IE, the properties of the Error object include name, number, description, message, and in the Firefox browser , the properties of the Error object include message, FileName, linenumber, Stack, name. In real-world applications where you want to implement your own exceptions, these properties are available only if they are assigned, and the Firefox browser automatically assigns a value to the Stack property to show where the exception appears.

JavaScript exception handling

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.