14th Chapter error Handling and debugging
1. Open Browser Error report
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. Error handling
2.1 Try-catch Statement
try{
Code that may cause errors
}catch (Error) {
What to do when an error occurs
}
-catch is quickly recycled to an object that contains an error message when an error occurs. There is a message attribute that is compatible with all browsers.
2.1.1 FINALLY clause
The Try-catch clause is optional, and the finally clause is used, and its code executes anyway.
2.1.2 Error Type
-error: base type. The primary purpose is for a developer to throw a custom error.
-evalerror: Thrown when an exception occurs with the Eval () function.
-rangeerror: Triggered when the value is outside the corresponding range.
-referenceerror: In case of an object not found
-syntaxerror: Pass the JavaScript string of the syntax error to the eval () function.
-typeerror: When an unexpected type is saved in a variable, or when a method that does not exist is accessed.
-urierror: Use encodeURI () or decodeURI (), and the URI is not in the correct format.
Error handling based on different types of errors:
try{
Soemfunction ();
}catch (Error) {
if (Error instanceof TypeError) {
Handling Type Errors
}else if (Error instanceof Referenceerror) {
Handling Reference Errors
}else{
Handling other types of errors
}
}
2.1.3 Use Try-catch
-Using the Try-catch statement, the browser will assume that the error has been processed.
-Use the Try-catch statement, the most appropriate to blow those we can not control the error.
-clearly know that your code is wrong should not use Try-catch but fix the error.
2.2 Throw Error
① has a throw operator that matches the Try-catch statement. The code stops executing immediately when the throw operator is encountered. The code continues to execute only if a Try-catch statement captures the value being thrown.
②throw New Error ("Something bad happened."); Or use a prototype chain to create a custom error type by inheriting error.
2.2.1 Throws the wrong time
2.2.2 Throw error and use Try-catch
2.2.3 Errors (Error) event
① any error that is not handled by Try-catch triggers the Error event for the Window object.
② onerror event handlers do not create event objects in any browser. However, 3 parameters can be accepted: Error message, URL and line number where the error is located. (only error messages are useful)
③ Specifies onerror event handlers that must use DOM0 level technology.
Window.onerror = function (message,url,line) {
alert (message);
return false; By returning false, this function actually acts as the Try-catch statement for the entire document, capturing all run-time errors that have no code handling.
};
The ④ image also supports the error event. The error event is triggered as long as the URL in the src attribute of the image cannot return an image format that can be recognized. The error event in this case follows the DOM format and returns an event object that targets the image.
3. Error handling strategy
3.1 Common types of errors
-Type Conversion Error
-Data type Error
-Communication error
3.1.1 Type conversion Error
A type conversion error occurs when using an operator, or by using another language structure that may automatically convert a value to a data type. The most common type conversion error occurs when using a non-Boolean value in the equality (= =) and inequality (!==) operators, or if, for, and while flow control statements.
3.1.2 Data type Error
Data type errors are most likely to occur when unexpected values are passed to the function.
3.1.3 Communication Error
The ①url format is incorrect or there is a problem with the data being sent. For query strings, you must use the encodeURIComponent () method.
② A communication error also occurs when the server responds to data that is incorrect.
3.2 Distinguishing between fatal and non-fatal errors
① Non fatal error
-Do not affect the user's primary tasks
-affects only part of the page
-Can be restored
-Repeat the same operation to eliminate errors
② Fatal Error
-The application does not run at all
-The error clearly affects the user's primary operation
-can cause other collateral errors
3.3 Log the error to the server: (slightly)
4. Debugging Technology
4.1 Logging messages to the console
For ①IE8, Firefox, Chrome, and Safari, you can write messages to the JavaScript console through the console object. Object has the following methods:
-error (message): Log error messages to the console
-info (message): Logging information messages to the console
-log (message): Log general messages to the console
-warn (message): Log warning messages to the console
Another option for ② is to run Java code using LiveConnect, or JavaScript.
③ a unified interface to write 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 Logging messages to the current page
4.3 Throw error
① for large applications, custom errors are usually thrown using the assert () function. Accept two arguments for this function, one for criteria that evaluates to true, and the other for errors that are 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 to must.");
return num1/num2;
}
5. Common IE errors
-Operation terminated
-Invalid characters
-No members found
-Unknown Run-time Error
-Syntax error
-The system cannot find the specified resource
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.