About JavaScript Exception Handling statements
Considering that JS errors are much more likely to be generated than the code on the server, and it is difficult to find and correct them, JS Code must have exception handling and global processing.
Errors are inevitable during the running of the program, and the running results after errors are often incorrect. Therefore, programs with errors during running are forcibly aborted. Running errors are collectively referred to as exceptions. In order to get a chance to handle errors, JavaScript provides Exception Handling statements. Include 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. The sequence of statements that may cause errors.
Exception: required. Any variable name used to reference the error object when an error occurs.
CatchStatements: Optional. Error Handling statement, used to handle errors in tryStatements.
During encoding, statements that may cause errors are often written into the curly brackets of the try block and the catch Block after the try block is used to handle errors. The Error message is contained in an Error object, which can be accessed through reference of exception. Based on the error information in the error object, determine if the error is handled.
?
| 1 2 3 4 5 6 7 8 |
<Script type = "text/javascript"> Try { Var n = error; // a human error is thrown. if the error is not defined, it is used. } Catch (e ){ Alert (e. number & 0 xFFFF) + "No. Error:" + e. description); // error handling: only error messages are output } </Script> |
This code snippet uses a try-catch structure to handle errors when running the program. 4th pedestrians cause an error. 6th ~ Catch Block errors of 9 rows and handle them.
Tip: JavaScript errors include runtime errors and syntax errors. syntax errors are found during compilation. While runtime errors are found during execution, error handling statements can only process runtime errors.
Try-catch-finally statement
?
| 1 2 3 4 5 6 7 8 9 |
Try { TryStatements; } Catch (exception ){ HandleStatements; } Finally { FianllyStatements; } |
Parameter description:
TryStatements: A required statement that may cause an exception.
HandleStatements: optional, exception handling statement.
FianllyStatements: optional statement that is unconditionally executed after other processes are executed.
Even if no errors occur, the statements in the finally block will be executed at the end. The program code for resource cleaning is usually placed here.
An exception is thrown when an array with an apple name is traversed.
?
| 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 & 0 xFFFF) + "No. Error:" + e. description ); } Finally { Fruit = null; Alert ("fruit =" + fruit + "has disconnected the reference of the fruit array! "); } </Script> |
Line 1 of the Code segment uses an undefined variable m, causing an exception. 11th ~ 13 rows capture and handle exceptions. 14th ~ The finally block of the 18 rows clears resources. This statement is executed unconditionally to prevent leakage of resources occupied by the fruit array.
Throw statement
Multiple Exception Handling statements can be nested. When multiple structures are nested, you can throw the try-catch statement in the lower layer if you do not want to handle exceptions by yourself. The parent try-catch statement can receive exceptions thrown by the child. throw statements are used to throw the exception.
Throw expression;
The expression value is transmitted as an error message object, which will be captured by the catch statement. Throw statements can be used wherever exceptions are to be thrown.
Generally, 0 cannot be used as the divisor. Therefore, an exception can be defined for the divisor of 0 and thrown.
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
<Script> Try { Var dividend = 100; // Divisor Var parts = 0; // Divisor If (parts = 0) {// if the divisor is 0, an exception is thrown. Throw "Error: parts is zero"; // throw an exception } Alert ("per person" + dividend/parts + "copy"); // output prompt information } Catch (e) {// The exception thrown in the try block will be caught here Alert (e); // use the dialog box to output the error Object Information } </Script> |
The above is all the content of this article. I hope you will like it.