I. Type of error
Error
Evalerror
Rangeerror
Referenceerror
SyntaxError syntax error
TypeError
Urierror
Error is a base type, and other error types inherit from the object, so all error types share the same set of properties. This type of error is rare.
Evalerror is thrown with an exception that occurs with the Eval () function, which is thrown when no eval () is used as a function.
Rangeerror
Values beyond a certain range are triggered and triggered when an object is not found, as well as when the variable is not declared.
TypeError
Type error, the most common type of error is the parameter passed to the function is not checked beforehand
Second, Try-catch statement
try{ leaf ()} catch (Error) { console.log (error.message);}
Leaf is not defined
Third, finally
Optional, but once used, its code will be executed anyway, even if the previous use of return
try{ return 1;} catch (Error) { return 2;} finally{ return 3;} Chrome under Error uncaught syntaxerror:illegal return statement
Iv. Throw Error Throw operation
When the throw symbol is encountered, the code executes immediately. The code will continue to execute only if the Try-catch statement captures the value that was thrown.
throw new Error (' Something Bad hanppened ');
V. Error events
Any error that is not handled by Try-catch will trigger the error event of the Window object. Whenever an error occurs, whether it is generated by the browser or not, the error event is triggered and the handler for the event is executed. IMG does not get the correct SRC will also be triggered.
Vi. Common types of errors
1. Type conversion error
Console.log (1==true)//true
Console.log (1==true)//false
In a judgment statement such as if (), any value is automatically converted to a Boolean value when the next step is performed.
Scenario: We recommend using the = = =!== operator
2. Wrong data type
Like what
Null and undefined use point operations
Use the reversed () method of sort () for non-array values
Scheme:
The base type (undefined null boolean string number) is detected with TypeOf;
Objects are detected with instanceof (note that the array is more special)
3. Communication error
Incorrect use of URLs and when the server responds with incorrect data
Resolution, sending the data to the server should use encodeURIComponent () to encode the data.
Vii. Summary
Ways to avoid browser responses to JavaScript errors
1. Use the Try-catch statement where the error may occur, so that there is an opportunity to respond to the error in an appropriate manner without having to follow the browser's error mechanism.
2, the Window.onerror event handler is used, this method can handle all errors that Try-catch cannot handle.
3, the main reason for the mistake of the book
Type conversion,
Data type not fully instrumented
Incorrect data sent to or received from the server
Error handling for JavaScript