1 Error types
- 1. Error is the base type other errors inherit from the type
- 2. Evalerror An exception occurs when using the Eval () function
- 3. Rangeerror
- 4. Referenceerror occurs when an object is not found (which causes object expected) to access a nonexistent object
- 5. SyntaxError syntax error
- 6. An unexpected type is stored in the TypeError variable, or when accessing a method that does not exist
- 7. Urlerror
Trycatch (error) {ifinstanceof TypeError) {// c Processing Type error Else if instanceof referenceerror) {// handling Reference error else {// handling other type error }}
2. There is also a throw operator that matches the Try-catch statement to throw a custom error at any time
function process (values) {ifinstanceof Array) {thrownew Error ("process (): argument must is an array"); // types of custom errors }values.sort (); for (var index = 0; index < values.length; index++) {if (Value[i] >) {
return
values[i];}}
return -1
;}
Try to focus on functions and factors that could lead to failure of function execution in JS development
3. Any error that is not handled by Try-catch will trigger the error event of the Window object. Only use DOM0-level events, that is, OnError accepts 3 parameters: Error message, URL and line number where the error is located (in most cases, only error messages are useful)
4. Image also supports error events
// Image Support Error Event var New image (); Image.addeventlistener (function (event) {alert ("Image not load" False// Specifies an image that does not exist
5 common types of errors in JS
- 1. Type conversion error: Occurs in the use of an operator (such as js (= =) and (! =) or if a non-boolean value in a Stream control statement
- 2. Data type error (before using variables and function parameters, to check if the data type will cause an error)
- 3. Communication error JS and the background server communication
- Note: Data is encoded using encodeurlcomponent () before sending data to the server (via URL)
6. Log the error to the server
Centrally save the error log while finding the cause of the error
//logging errors to the serverfunctionlogError (SEv, Meg) {//The first parameter describes the severity of the error, the type of the MEG value error, and the information that is customizedvarIMG =NewImage (); IMG.SRC= "log.php?sev=" + encodeuricomponent (SEv) + "&meg=" +encodeURIComponent (MEG);}varArray = []; for(varindex = 0; Index < Array.Length; index++) {Try{array[i].init ();}Catch(Error) {LogError ("Nonfatal", "Module Init failed:" +error.message);}}
7.console objects have the following methods:
- Error message (message) is logged in the console
- info (message) logs informational messages to the console
- Log (message) logs generic messages to the console
- Warn (message) warns the message
8.//generally for large program custom errors are thrown by the assert () function
function assert (condition, message) {if (! Condition) {thrownew Error (message);}} // Use function Divide (NUM1, num2) {assert (typeoftypeof num2 = = "Number", "Divide (): both arguments Must be Numbers "); return NUM1/ num2;}
9. Unknown run errors (Unknown runtime error) occur when you specify HTML using innerHTML and outerHTML:
- 1. Inserting block-level elements into the inline element
- 2. Access any part of the form (e.g. <table> <tbody>)
Error handling in JS