Let's take a look at an example
<input id= ' b1 ' type= ' button ' value= ' buttons '/>
<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 that:
AddEventListener and attachevent difference in the first argument the former is click the latter is the onclick
AddEventListener is running in the element scope where the element is located
Attachevent Run in global scope (This=window)
Try ... Catch statement
Try...catch can test errors in your code. The try section contains the code that needs to be run, and the catch part contains the code that runs when the error occurs.
Grammar:
Try
{
//Run code
}
catch (Err)
{
//Handle errors here
}
Note: Try...catch uses lowercase letters. Uppercase letters can go wrong.
try...catch...finally statement
Implement error handling for JScript.
Try {
trystatements}
catch (Exception) {
catchstatements}
finally {
Finallystatements}
=============
Parameters
Trystatement
Required option. The statement that the error may occur.
exception
Required option. Any variable name. The initialization value of the exception is the value of the error thrown out.
Catchstatement
Options available. A statement that handles errors that occur in the associated trystatement.
Finallystatements
Options available. A statement that executes unconditionally after all other processes occur.
Description
The try...catch...finally statement provides a way to handle some or all of the errors that can occur in a given block of code while still keeping the code running. If there is an error that the programmer has not handled, JScript simply gives the user its normal error message, as if there were no error handling.
The trystatements parameter contains code that may have an error, and Catchstatement contains code to handle any errors that occurred. If an error occurs in the trystatements, the program control is passed to catchstatements for processing. The initialization value of the exception is the value of the error that occurred in trystatements. If the error does not occur, the catchstatements is not executed.
If the error is not handled in the catchstatements associated with the trystatements in which the error occurred, the throw statement is used to propagate, or to throw back the error, to a more advanced error handler.
The statements in finallystatements can be executed unconditionally after the statements in Trystatements are executed and after all error handling of catchstatements occurs.
Note that even if you return a statement in a try or catch block, or if you throw an error back in the catch block, the finallystatements encoding is still performed. It is generally ensured that the finallystatments is running unless an unhandled error exists. (for example, a run-time error occurs in a catch block.) )。
Example
The following example illustrates how JScript exception handling is done.
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");
} Windows Script Host makes the modification to derive WScript.Echo (s)
function Print (s) {
document.write (s);
}
The following results will be drawn:
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 exception handling for JavaScript.
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. array[0] When an array is not created and an array is an empty object, calling Array[0 in a program produces an exception with object is null
2. The catch (ERR) statement captures the exception by err.name the error type, Err.message prints the error details.
3. Finally, the Java-like finally, regardless of whether there is no exception will be executed.
Summary of the Error.name six values corresponding to the information:
1. The use of Evalerror:eval () is inconsistent with the definition
2. Rangeerror: Value out of bounds
3. Referenceerror: Illegal or unrecognized reference value
4. SyntaxError: Syntax resolution error occurred
5. TypeError: Error of operand type
6. Improper use of urierror:uri processing function