Under what circumstances is JStrycatch used? Is it required? The following is a brief introduction to the trial experience. First look at an instance
Script window. onload = function () {var oBtn = document. getElementById ("b1"); function mto () {alert ("123") ;}; try // non-IE {oBtn. attachEvent ("onclick", mto, false);} catch (e) // IE {oBtn. addEventListener ("click", mto, false) ;}}; script
Note:
The difference between addEventListener and attachEvent is that the first parameter is click and the second parameter is onclick.
AddEventListener runs in the element scope of its element
AttachEvent runs in global scope (this = window)
Try... Catch statement
Try... catch can be used to test errors in the code. The try part contains the code to be run, and the catch part contains the code that runs when an error occurs.
Syntax:
Try {// run the code here} catch (err) {// handle the error here}
Note: try... catch uses lower-case letters. An error occurs when uppercase letters are used.
Try... catch... finally statement
Implements error handling for JScript.
try { tryStatements} catch(exception){ catchStatements} finally { finallyStatements}
==================
Parameters
TryStatement
Required. An error may occur.
Exception
Required. Any variable name. The initialization value of exception is the thrown error value.
CatchStatement
Optional. The statement that handles errors in the associated tryStatement.
FinallyStatements
Optional. The statements that are unconditionally executed after all other processes occur.
Description
The try... catch... finally statement provides a way to handle some or all errors that may occur in a given code block while keeping the code running. If an error is not handled by a programmer, JScript only provides the user with its common error information, as if there is no error processing.
The tryStatements parameter contains code that may cause errors, while the catchStatement parameter contains code that handles any errors. If an error occurs in tryStatements, program control is passed to catchStatements for processing. The initialization value of exception is the error value in tryStatements. If the error does not occur, catchStatements is not executed.
If the error cannot be handled in the catchStatements associated with the tryStatements with which the error occurs, use the throw statement to spread the error or re-throw the error to a more advanced error handler.
After you have executed the statements in tryStatements and processed all errors in catchStatements, You can unconditionally execute the statements in finallyStatements.
Please note that the finallyStatements code will be executed even if a statement is returned in the try or catch block or an error is thrown again in the catch Block. The finallyStatments operation is generally ensured, unless there are unhandled errors. (For example, a runtime error occurs in the catch Block .).
Example
The following example illustrates how JScript special case processing is performed.
Try {print ("Outer try running .. "); try {print (" Nested try running... "); throw" an error ";} catch (e) {print (" Nested catch caught "+ e); throw e +" re-thrown ";} finally {print ("Nested finally is running... ") ;}} catch (e) {print (" Outer catch caught "+ e);} finally {print (" Outer finally running ");} // modify the Windows Script Host to obtain the WScript. echo (s) function print (s) {document. write (s );}
The following results are obtained:
Outer try running ..
Nested try running...
Nested catch caught an error
Nested finally is running...
Outer catch caught an error re-thrown
Outer finally running
The following is an example of Javascript exception handling.
var array = null;try { document.write(array[0]);} catch(err) { document.writeln("Error name: " + err.name + ""); document.writeln("Error message: " + err.message);}finally{ alert("object is null");}
Program Execution Process
1. When array [0] is created, array is an empty object because array [0] is not created. If array [0] is called in the program, the object is null.
2. The catch (err) statement captures this exception and prints the Error Type through err. name, and err. message prints the error details.
3. finally is similar to java finally, and will be executed no matter whether exceptions exist.
We will summarize the information corresponding to the six values of Error. name:
1. EvalError: eval () is used and defined differently.
2. RangeError: value out of bounds
3. ReferenceError: Invalid or unrecognized reference value
4. SyntaxError: Syntax Parsing Error
5. TypeError: Incorrect operand type
6. URIError: The URI processing function is improperly used.