In JavaScript you can use Try...catch to do exception handling. For example:
Try { foo.bar ();} Catch (e) { alert (e.name + " : " +
The system anomalies we may get at the moment mainly include the following 6 types:
evalerror:raised when a error occurs executing code in eval ()
rangeerror:raised when a numeric variable or parameter are outside of its valid range
referenceerror:raised when de-referencing an invalid reference
syntaxerror:raised when a syntax error occurs and parsing code in eval ()
typeerror:raised when a variable or parameter are not a valid type
urierror:raised when encodeURI () or decodeURI () is passed invalid parameters
All six of the above exception objects inherit from the Error object. They all support the following two methods of construction:
New Error ();
New Error ("Exception information");
The methods for throwing exceptions manually are as follows:
Try { throw new "" );} Catch (e) { alert (e.name + " : " + e.message);}
To determine the type of exception information, you can judge it in a catch:
Try { foo.bar ();} Catch (e) { if (e instanceof evalerror) { alert ( E.name + " : " + e.message); } Else if (e instanceof rangeerror) { alert (e.name + " ) : " + e.message); } // }
Error has some of the following main properties:
Description: Error description (ie only available).
FileName: The file name of the error (only Mozilla is available).
LineNumber: Number of rows in error (only Mozilla is available).
Message: Error message (description in IE)
Name: the error type.
Number: Error code (IE is available only).
Stack: Error stack information like stack trace in Java (only Mozilla is available).
So in order to better understand the error message we can change the catch part to the following form:
Try{foo.bar ();} Catch(e) {if(BrowserType! =Browser_ie) {Alert ("Name:"+ E.name +"\nmessage:"+ E.message +"\nlinenumber:"+ E.linenumber +"\nfilename:"+ E.filename +"\nstack:"+e.stack); } Else{alert ("Name:"+ E.name +"\nerrornumber:"+ (E.number &0xFFFF) +"\nmessage:"+ E.message" ); } }
The throw command in JavaScript can actually throw any object, and we can accept that object in a catch. For example:
Try { throw new Date (); // } catch (e) { alert (e.tolocalestring ()); // }
JavaScript exception snapping