The sharing of exception handling methods in JS

Source: Internet
Author: User
Tags current time error code eval exception handling stack trace valid
We are in the process of writing JS, will inevitably encounter some code error problems, need to find out, and sometimes because of the JS problem caused by poor user experience, here are some solutions to JS fault-tolerant statements, JS error is not prompted error (to prevent the browser in the lower right corner of a yellow triangle symbol, or user experience is not good)     Code as follows: Window.onerror=function () {return true;}         Below is to get the JS exception information to facilitate the developer to retrieve the problem   1,try...catch ...   code as follows: <script type= "text/ JavaScript "> var txt=" "function message () {try    {   adddlert (" Welcome guest! ")    } catch (Err)    {     txt= "There are errors in this page. nn "     txt+=" click "OK" to continue to view this page, n "     txt+=" click "Cancel" to return to the home page. nn "     if (!confirm (TXT))          {         DOCUMENT.L ocation.href= "/index.html"              } </script>     2, throw &NB Sp   Code as follows: <script type= "Text/javascript" > var x=prompt ("Please enter number from 0 to 10:" "") try {if (x>10)   throw "Err1 "Else If" (x<0)   throw "Err2" else if (isNaN (x))   throw "Err3"} catch (er) {if (er== "ERR1")   alert ("Error!") The value is too large! ") if (er = =" ERR2 ")   alert (" Error! "). The value is too small! ") if (er = =" ERR3 ")   alert (" Error! "). The value is not a number! ")} </script>     3,onerror:   Code as follows: <script type=" Text/javascript "> Onerror=handleerr var txt = ""   function Handleerr (msg,url,l) {txt=) there is an error on this page. nn "txt+=" error: "+ msg +" n "txt+=" url: "+ URL +" n "txt+=" line: "+ L +" nn "txt+=" click "OK" to continue. nn "alert (TXT) return true}   function message () {Adddlert (" Welcome guest! ")} </script>       In JS, exception handling     in JavaScript can use Try...catch for exception handling. 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 of:    evalerror:raised when a error occurs executing code in eval ()   Rangeerr or:raised when a numeric variable or parameter are outside of it valid range   referenceerror:raised when De-refere Ncing a invalid reference   syntaxerror:raised When a syntax error occurs while parsing coDe in eval ()   typeerror:raised when a variable or parameter are not a valid type   urierror:raised when encode The six exception objects above the URI () or decodeURI () are passed invalid parameters   inherit from the Error object. They all support the following two construction methods:    new error (); New error ("Exception information");  manually throws an exception as follows: The   code is as follows: Try {  throw new error ("Wh Oops! ");} catch (E) {  alert (e.name + ":" + E.message);}       If you want to determine the type of exception information, you can judge it in a catch:         code as follows: 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 key attributes:    Description: Error description (ie only available).   FileName: Wrong filename (Mozilla only available).   linenumber: Number of rows (Mozilla only available).   Message: Error messages (in IE below description)   Name: Error type.   Number: Error code (ie available only).   Stack: Error stack information like stack trace in Java (Mozilla only available).   So in order to better understand the error message, weYou can change the catch section to the following form:          code as follows: try {    foo.bar (), catch (E) {    if (bro Wsertype!= 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 at catch." For example:        code as follows: try {    throw new Date ();//Throw Current Time object} catch (e) {alert (e.tolocalestring ()); Display current time    }     in local format
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.