JavaScript advanced programming error handling and debugging learning notes

Source: Internet
Author: User

Chapter 3 error handling and debugging
1. Enable browser error reporting
1.1 Internet Explorer
□Toos → Internet Option → Anvanced → Display a notification about every script error
1.2 Firefox
□Tools → Error Console (Firebug)
1.3 Safari
□Edit → Preferences → Advanced → Show develop menu in menubar/Develop → Show Error Console
1.4 Opera
□Tools → Advanced → Error Console
1.5 Chrome
□Control this page → Developer → JavaScript console
2. handle errors
2.1 Try-catch clause
Try {
// Code that may cause errors
} Catch (error ){
// How to handle errors
}
□When an error occurs, catch quickly recycles an object containing the error information. There is a message attribute compatible with all browsers.
2.1.1 finally Clause
The try-catch clause is optional. Once a finally clause is used, its code is executed no matter how it is executed.
2.1.2 Error Type
□Error: base type. The main purpose is for developers to throw custom errors.
□Evalerror: thrown when an exception occurs using the eval () function.
□Rangeerror: triggered when the value exceeds the corresponding range.
□Referenceerror: the object cannot be found.
□Syntaxerror: Pass the JavaScript string with incorrect syntax into the eval () function.
□Typeerror: When an unexpected type is stored in the variable or the method that does not exist is accessed.
□Urierror: When encodeURI () or decodeURI () is used, and the URI format is incorrect.
Handle Errors Based on different error types:
Try {
SoemFunction ();
} Catch (error ){
If (error instanceof TypeError ){
// Processing Type Error
} Else if (error instanceof ReferenceError ){
// Handle reference errors
} Else {
// Handle other types of errors
}
}
2.1.3 use try-catch
□When using the try-catch statement, the browser will think that the error has been handled.
□Use the try-catch Statement, which is most suitable for blowing errors that we cannot control.
□If you clearly know that your code has an error, you should not use try-catch instead of fixing the error.
2.2 throw an error.
① There is a throw operator that matches the try-catch statement. When the throw operator is encountered, the Code immediately stops execution. The code will continue to be executed only when a try-catch statement captures the thrown value.
② Throw new Error ("something bad happened."); or use the prototype chain to inherit Error to create a custom Error type.
2.2.1 when an error is thrown
2.2.2 throw an error and use try-catch
2.2.3 error event
① Any errors not handled through try-catch will trigger the error event of the window object.
② In any browser, the onerror event handler does not create event objects. However, three parameters are acceptable: the error message, the URL of the error, and the row number. (Only error messages are useful)
③ Specify the onerror event handler, which must use the DOM0-level technology.
Window. onerror = function (message, url, line ){
Alert (message );
Return false; // if false is returned, this function actually acts as a try-catch statement for the entire document to capture all runtime errors without code processing.
};
④ Images also support error events. An error event is triggered as long as the URL in the src feature of an image cannot return a recognizable image format. In this case, the error event follows the DOM format and an event object with the image as the target will be returned.
3. Error Handling Policy
3.1 common error types
□Type Conversion error
□Data Type Error
□Communication Error
3.1.1 type conversion error
A type conversion error occurs when an operator is used, or other language structures of data types that may automatically convert values are used. In use equal (=) and not equal (! =) Operator, or if, for, while, and other flow control statements that use non-boolean values, the most common type conversion error occurs.
3.1.2 incorrect data type
When unexpected values are passed to the function, data type errors are most likely to occur.
3.1.3 communication error
① The URL format is incorrect or the sent data is incorrect. The encodeURIComponent () method must be used for query strings.
② Communication errors may also occur when the server response data is incorrect.
3.2 differentiate between fatal and non-fatal errors
① Non-fatal error
□Main tasks of the user are not affected
□Only part of the page is affected
□Recoverable
□Repeated operations can eliminate errors
② Fatal error
□The application cannot run at all
□The error significantly affects the user's main operations
□Other associated errors
3.3 record errors to the server: (omitted)
4. debugging technology
4.1 Record messages to the console
① For IE8, Firefox, Chrome, and Safari, messages can be written to the JavaScript console through the console object. The object has the following methods:
□Error (message): record the error message to the console
□Info (message): record information messages to the console
□Log (message): record general messages to the console
□Warn (message): record the warning message to the console
② Another solution is to use LiveConnect, that is, to run Java code in JavaScript.
③ Unified interface for writing messages to the JavaScript console:
Function log (message ){
If (typeof console = "object "){
Console. log (message );
} Else if (typeof opera = "object "){
Opera. postError (message );
} Else if (typeof java = "object" & typeof java. lang = "object "){
Java. lang. System. out. println (message );
}
}
4.2 record messages to the current page
4.3 throw an error.
① For large applications, custom errors are usually thrown using the assert () function. Two parameters are accepted for this function. One is the condition that the result is true, and the other is the error to be thrown when the condition is false.
Function assert (condition, message ){
If (! Condition ){
Throw new Error (message );
}
}
② Application:
Function divide (num1, num2 ){
Assert (typeof num1 = "number" & typeof num2 = "number", "divide (): Both arguments must be numbers .");
Return num1/num2;
}
5. Common IE errors
□Operation termination
□Invalid characters
□No member found
□Unknown runtime error
□Syntax Error
□The system cannot find the specified resource

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.