On JavaScript exception Handling statements

Source: Internet
Author: User
Tags error handling exception handling finally block throw exception

Considering the error in JS can be more than the server side of the code generated more errors, and also difficult to find and modify, so the JS code must have exception handling as well as a global deal.

The operation of the program will inevitably make mistakes, errors after the results are often incorrect, so the run-time error of the program is usually forced to abort. A run-time error is collectively called an exception, and JavaScript provides an exception-handling statement in order to be given an opportunity to handle errors as they occur. Contains Try-catch, try-catch-finally, and throw.

Try-catch statement

?

1 2 3 4 5 6 try{trystatements} catch (Exception) {catchstatements}

Parameter description:

trystatements: Required option. A sequence of statements that may be wrong.

Exception: Required option. Any variable name that refers to the Error object when the error occurred.

Catchstatements: Options available. Error-handling statements that are used to handle errors that occur in trystatements.

When encoding, statements that are likely to have errors are usually written to the curly braces of the try block, and errors are handled in subsequent catch blocks. The error message is contained in an Error object (the Error object) that can be accessed through a exception reference. Depending on the error message in the Error object, determine if processing.

?

1 2 3 4 5 6 7 8 <script type= "Text/javascript" > try{var n = error;///////////////////////// + "Number error:" + e.description); Error handling: Output only error messages} </script>

The code snippet uses a Try-catch structure handler run-time error, and the 4th pedestrian raises an error. The Catch block on line 6th to 9th catches the error and handles it.

Tip: JavaScript errors are run-time and grammatical errors, syntax errors are discovered at compile time, and run-time errors are found during runtime, and error-handling statements can only handle run-time errors.

try-catch-finally statement

?

1 2 3 4 5 6 7 8 9 try{trystatements} catch (Exception) {handlestatements;} finally{fianllystatements;}

Parameter description:

Trystatements: Required option, the statement that might throw an exception.

Handlestatements: Optional, exception handling statement.

Fianllystatements: Optional statements that are executed unconditionally after the execution of other procedures.

Although no errors occur the statements in the finally block are also executed at the end, where the program code for resource cleanup is usually placed.

An exception is thrown when traversing an array with an Apple name.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 <script type= "Text/javascript" > try{var fruit = new Array ("Pear", "apple", "grape", "plum"); for (n=0;n<fruit.length;m++) {document.write (Fruit[n] + "");} catch (E) {alert ((E.NUMBER&0XFFFF) + "error:" + e.description);} finally{fruit = null; Alert ("fruit=" +fruit+ has disconnected f Ruit an array of references! "); } </script>

The code snippet 5th exercises an undefined variable m and throws an exception artificially. Line 11th to 13th catches the exception and handles it. The finally block on line 14th to 18th clears the resource, and the statement is executed unconditionally, ensuring that the resources of the fruit array are not compromised.

Throw statement

Multiple exception handling statements can be nested. When more than one structure is nested, it can be thrown if the inner Try-catch statement does not intend to handle the exception itself. The parent Try-catch statement can receive exceptions thrown by the child, and the throw operation uses the throw statement.

Expression of throw;

The value of an expression is outgoing as an error message object that will be caught by a catch statement. The throw statement can be used anywhere that you intend to throw an exception.

Typically 0 cannot be a divisor, so you can define an exception for the divisor of 0 and throw it.

?

1 2 3 4 5 6 7 8 9 10 11 12-13 <script> try{var dividend = 100;//dividend var parts = 0;//divisor if (parts = 0) {//If divisor is 0 throws exception throw "Error:parts is Zer O "; Throw exception} alert ("per person" + dividend/parts + "copy"); Output hint Info} catch (e) {//This will catch the exception alert (e) thrown in the try block;/////Output Error object Information in dialog box </script>

The above mentioned is the entire content of this article, I hope you can enjoy.

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.