JS Exception Handling

Source: Internet
Author: User
Tags stack trace

<script language= "JavaScript" > try {throw new Error (Ten, "ASDASDASD")} catch (e) {alert (e.message); alert (E.descrip tion) alert (e.number) alert (e.name) throw new Error (Ten, "ASDASDASD")} </script>  
In JavaScript you can use Try...catch to do exception handling. For example: try {foo.bar ();} catch (E) {alert (e.name + ":" + E.message);} The system anomalies we may get now include the following 6 types: evalerror:raised When an error occurs executing code in eval () rangeerror:raised when a numeric V Ariable or parameter is outside of it valid range referenceerror:raised when de-referencing an invalid reference Synta xerror:raised when a syntax error occurs while parsing code in eval () typeerror:raised when a variable or parameter is Not a valid type urierror:raised when encodeURI () or decodeURI () is passed invalid parameters the above six exception objects are inherited from the Error object.  They all support the following two construction methods: New Error (), new error ("Exception information"), the method of throwing the exception manually: try {throw new error ("Whoops!"),} catch (e) {alert (e.name + ":") + e.message);} To determine the type of the exception information, it can be judged in the 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 main properties: Description: Error description (ie only available). FileName: The file name of the error (MO onlyZilla available).  LineNumber: Number of rows in error (only Mozilla is available).  Message: Error message (description in IE) Name: 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 + "message:" + E.message + "linenumber:" + E.linenumber + "FileName:            "+ E.filename +" stack: "+ e.stack);       } else {alert ("Name:" + e.name + "ErrorNumber:" + (E.number & 0xFFFF) +            "Message:" + e.message "); The throw command in}javascript can actually throw any object, and we can accept this object in a catch. For example: try {throw new Date ();//Throw the current Time object} catch (e) {alert (e.tolocalestring ());//display current time in local format}


Instance
1 //JS Call App interface action method name param parameter JSON string2 functionInterface (action) {3     Try{4         //get access terminal type5         varDevicetype=Getdevicetype ();6         varPARAM=ARGUMENTS[1]?ARGUMENTS[1]: ";7         if(devicetype== ' Android '){8 javascriptinterface[action] (param);9}Else if(devicetype== ' iOS '){Ten Callfun (action,param); One}Else{ AAlert ("Method name:" +action+ ", Parameter:" +param); -         } -}Catch(e) { the alert (e.message); -     }     -}

JS Exception Handling

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.