<script language= "JavaScript" >
Try
{
throw new Error ("ASDASDASD")
}
catch (E)
{
alert (e.message);
Alert (e.description)
Alert (E.number)
Alert (e.name)
throw new Error ("ASDASDASD")
}
</script>
In JavaScript, you can use Try...catch for exception handling. For example:
try {
foo.bar ();
} catch (e) {
alert (e.name + ":" + e.message);
}
At present, we may get the system anomaly mainly contains the following 6 kinds: evalerror: Raised when a error occurs executing code in eval () rangeerror: Rai sed when a numeric variable or parameter is outside of it valid range Referenceerror: Raised when De-referenci Ng an invalid reference SyntaxError: Raised when a syntax the error occurs while parsing the code in eval () Typee Rror: Raised when a variable or parameter are not a valid type Urierror: Raised when encodeURI () or Decodeu RI () are 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 way to throw exceptions manually is as follows:
try {
throw new Error ("whoops!");
} catch (e) {
alert (e.name + ":" + e.message);
}
To determine the type of exception information, you can judge 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);
}
Etc
}
Error has some of the following primary properties: Description: Error description (ie only available). FileName: The file name of the error (Mozilla only available). LineNumber: The number of rows that failed (Mozilla only available). Message: Error information (in IE below description) Name: Error type. Number: Error code (ie only available). 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 section to the following form:
try {
foo.bar ();
} catch (e) {
if (BrowserType!= browser_ie) { alert (" Name: "+ e.name + " message: "+ e.message + " linenumber: "+ e.linenumber + " Filenam E: "+ e.filename + " stack: "+ e.stack);
}
else { alert ("Name:" + e.name + "errornumber:" + (E.number & 0xF) FFF) + "message:" + e.message ");
}
}
The throw command in JavaScript can actually throw any object, and we can accept this object at catch. For example:
try {
throw new date ();//Throw Current time object
} catch (e) {
alert (e.tolocalestring ());//show current time using local format
}