JS Error Handling and debugging

Source: Internet
Author: User

In the development of the program will inevitably encounter some errors, in thousands of code to find errors is clearly equivalent to a needle in the haystack, for this reason, each computer programming language has its unique set of error handling and debugging mechanism. Of course, JavaScript is no exception.


Error occurred:


There are many errors that occur when executing code. Each of these errors has a corresponding error type. ECMA-262 defines 7 types of errors:

1.Error Error

2.EvalError Global Error

3.RangeError Reference Error

4.ReferenceError parameter Error

5.SyntaxError syntax error

6.TypeError type Error

7.URIError encoding Error


Where error is a base type, other types inherit from it. The error type is rarely seen and is usually thrown by the browser. The Evalerror type means that the global function eval () is used differently from the definition, but it does not actually produce this error, so there is little likelihood of actually encountering it. When using encodeURI () and decodeURI (), a urierror error is caused if the URI is not properly formatted. But because of the very strong URI compatibility, this error is almost invisible.


Handling Errors:


The browser itself has the function of error. In the case of IE, an error will pop up when the Debug box is wrong. But this requires script debugging, which is set to: Tools->internet options, advanced, disable script debugging, uncheck it. Other browsers are similar.

A good error-handling mechanism can prompt users to know what's going on without panicking, and as a developer, it's important to understand what means and tools are available when dealing with JavaScript errors.


The Try-catch statement.

Example:

try {                                           //attempt to execute the code contained in the try     WINDOW.ABCDEFG ();                          Non-existent method} catch (e) {                                   //If there is an error, execute Catch,e is the exception object     alert (' An error has occurred, the error message is: ' + e);    Direct printing calls the ToString () method}


It is not recommended to use Try-catch by modifying code or browser-compatible errors, because it consumes more resources and is more burdensome than the general statement. Therefore, in the last resort, unable to modify the code, not through the general judgment of the case to use Try-catch.


Finally clause

Example:

try {     window.abcdefg ();} catch (e) {     alert (' An error occurred, error message: ' + e.stack);} finally {                                            //will always be executed     alert (' I will execute! ');}


Finally, the function is generally to prevent the occurrence of an exception, can not be performed after the standby.

Throw error

Use catch to handle the error message, and if you can't handle it, we throw it away.

Example:

try {
     New 10;} catch (e) {     if (e instanceof TypeError) {        throw new TypeError (' instantiated type causes an error! '); Direct Chinese explanation error message} else {<span style= "White-space:pre" ></span>throw new Error (' Throws an unknown error! ');}}

Debug Error:


In the early days of JavaScript, browsers did not provide debugging tools for JavaScript, so developers came up with a set of their own debugging methods, such as alert (). But using alert () to debug the error is cumbersome, need to be repeated cut and paste, if you forget to delete the alert for debugging () will be particularly headache, now we have a better debugging method.


Logging messages to the console

Example:

1.

Console.error (' Error! ');    Red with fork Console.info (' info! ');     White with information number Console.log (' Log! ');     White Console.warn (' Warning! ');    Yellow with exclamation marks
2.

var num1 = 1;console.log (typeof num1);    Get NUM1 type var num2 = ' B '; Console.log (typeof num2);   Gets the type of num2 var result = Num1 + Num2;alert (result);              The result is 1b, unbelievable.


Comparison with alert (): Because alert () will block the execution of the code behind it, and then delete it after reading it, and then forget about it after deleting the estimate. If the use of Congsole.log, all the variables to debug at a glance, do not need to delete, it is OK to put.


Throw error


There are examples and explanations of throwing errors, which are not mentioned here.


Debugging Tools


Browsers have their own debugging tools, and developers are only accustomed to the Firefox one, so in many cases, in Firefox development debugging, and then go to other browsers to do compatibility. In fact, Firebug tool provides a web version of Debugging Tools: Firebug Lite.


Debugging steps

1. Set breakpoints

2, one-step debugging: Five buttons

Rerun: Re-step debugging

Break: Normal Execution code

Step into: Step-by-step process execution

Step Skip: Skip to Next function block

Stepping out: Stepping out to the inside function

3. Monitoring

4. Control Station


Summary:

Error handling and debugging before VB,C++,C#,VB. NET has a certain degree of learning and understanding. But the points of knowledge are scattered. In JavaScript there is a macroscopic and detailed control of it, with a deeper impression of it. Knowledge is to be learned over and over again, from unfamiliar to familiar to proficient, is a gradual process. Error handling and debugging the mechanism has been learning for so long that it can be flexibly applied to various projects and systems in the future.









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JS Error Handling and debugging

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.