JS try-catch application __js

Source: Internet
Author: User
Tags exception handling stack trace

<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 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.