Page 1/2 of JavaScript Fault Tolerance Exception Handling

Source: Internet
Author: User
Tags finally block

Many people who want to know how to use the Code directly come to their feet. Here we will give common error-tolerant Code directly.
First, the home of the feet is also in use.
Copy codeThe Code is as follows:
<SCRIPT language = javascript>
<! --
Window. onerror = function () {return true ;}
// -->
</SCRIPT>


For more information, see javascript error tolerance code (blocking js errors)

For more details, go on to the page.
If the exception handling code is well-designed, the final presentation to the user will be a friendly interface. Otherwise, visitors will be surprised by the inexplicable phenomenon ". This article will introduce you to the Exception Processing Technology of the JavaScript language in Web development.

1. What is exception handling?

When the JavaScript program is running, such as the array index out-of-bounds, Type mismatch, or syntax error, the JavaScript interpreter will cause exception processing. ECMAScript defines six types of errors. In addition, we can use the Error object and throw statement to create and trigger custom exception handling information.

Ii. Advantages of Exception Handling Technology

By using the exception handling technology, we can respond to error events in a structured manner, so that the exception handling code can be scientifically separated from Normal script code, in the end, we were able to focus on writing core programs that completed the main functions.


3. Use try... Catch... Finally executes Exception Handling

In JavaScript, we use try... Catch... The finally statement is used to execute Exception Processing, that is, to capture exceptions caused by errors or exceptions generated by executing throw statements. Its basic syntax is as follows:
Copy codeThe Code is as follows:
Try {
// Exception statements may be generated here
} Catch (error ){
// Here is the statement for Exception Handling
} Finally {// here is the exit statement
}

In the above Code, the statements in the try block are first executed. If an error occurs during running, the control is transferred to the statement located in the catch block. The error parameter in the brackets is passed as the exception variable. Otherwise, the catch BLOCK statement is skipped and not executed. Whether the execution of the statements in the catch block is completed when an error occurs or the execution of the statements in the try block is not completed, the statements in the finally block are executed.

Here is an example:

After executing the above Code in the browser, the input dialog box is displayed:

Input abc and confirm. The output result is as follows:
 
"Start to execute the try BLOCK statement ---> no exceptions yet ---> catch the exception and start to execute the catch Block statement ---> error name: TypeError ---> error message: 'abc' undefined ---> start to execute the finally BLOCK statement"

The preceding example starts with a try BLOCK statement. When the output information "no exceptions have occurred", the input dialog box is displayed, asking the user to enter a value. When the input information "abc" is invalid, an exception is thrown, so the statements in the remaining try block will be skipped and the catch BLOCK statement will be executed. The err parameter starting with the Catch block is the exception object. It has two attributes: name and message. Finally, execute the finally BLOCK statement.

Run the code again and enter a correct value of 123:

You will see the following results:

As shown in the preceding figure, the catch BLOCK statement is skipped after the try BLOCK statement is executed. A window displays the input value and finally the finally BLOCK statement is executed.

4. try... catch... finally Deformation

Try... Catch... The finally statement has two types of deformation applications: try... Catch or try... Finally.

Try... The catch structure is the most common. Its Execution Process is: when no exception occurs, the try BLOCK statement is executed, or the catch Block statement is executed after the exception occurs, the control will be transferred to the entire try... The statement after the catch structure. See the following example:
 
Try {
Document. writeln ("Beginnng the try block ")
Document. writeln ("No exceptions yet ")
// Create a syntax error
Eval ("6 + * 3 ")
Document. writeln ("Finished the try block with no exceptions ")
} Catch (err ){
Document. writeln ("Exception caught, executing the catch block ")
Document. writeln ("Error name:" + err. name)
Document. writeln ("Error message:" + err. message)
}
Document. writeln ("Executing after the try-catch statement ")

For try... In the finally structure, when exceptions occur, the finally BLOCK statement will not be executed because no BLOCK statement is caught to capture errors. Therefore, this structure is rare in practical applications.

V. Exception format: Error object

In JavaScript, exceptions occur as Error objects. The Error object has two attributes: The name attribute indicates the exception type, and the message attribute indicates the meaning of the exception. Based on the values of these attributes, we can decide how to handle exceptions, such:

Function evalText (){
Try {
Alert (eval (prompt ("Enter JavaScript to evaluate :","")))
} Catch (err ){
If (err. name = "SyntaxError") alert ("Invalid e-xpression ")
Else alert ("Cannot evaluate ")
}
}

The above code evaluates the expression of the content entered by the user and then displays it. If a SyntaxErroe type error occurs during the evaluation, the information of "Invalid e-xpression" is displayed. Otherwise, the user obtains the information "Cannot evaluate ".

There are six Error. name values:

EvalError: eval () usage and definition are inconsistent
RangeError: value out of bounds
ReferenceError: an invalid or unrecognized reference value
SyntaxError: Syntax Parsing Error
TypeError: Incorrect operand type
URIError: The URI processing function is improperly used.

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.