Error object
Property:
name: error name
number: error number
description: description
message: error message, multiple descriptions
FF Only property
fileName: The file where the error occurred
stack: The call stack when the error occurred
Constructor:
Error(){
this(0,"")}
Error(description){
this(0,description)}
Error(number,description){
....}
Constructor parameter without name , because the name of the Error object corresponds to its source:
EvalError: The error occurred in eval()
SyntaxError: Syntax error, the error occurred in eval(), because a SyntaxError occurring at other points cannot be explained Device
RangeError: Value out of range
ReferenceError: Reference is not available
TypeError: Variable type is not expected
URIError: Error occurred in encodeURI() or decodeURI()
throw Throw Error:
throw new Error(0,"Error Demo");
new Error can be omitted:
throw("Error Demo");
Capture Error:
try catch finally statement:
try{
..possibly wrong statement..}
catch(e){
..processing after error occurs..}
finally{
..The statement block executed after completion..}
Finally is not necessary
If nested, do not use the same parameter name for the two catches to avoid overwriting
The parameter passed in is an Error object, which can be Get error information from it
FF supports one try with multiple catches, because JS is a weak type and is not recommended to use
window.onerror error capture:
window.onerror=function(Msg,Url,Num) {}
The onerror event will be passed to the callback function with 3 default parameters
Msg: Error message
Url: Url of the file where the error occurred
Num: Line number where the error occurred
window. onerror can also handle SyntaxError, which is more powerful than try catch.
But onerror belongs to BOM, so each browser manufacturer supports it differently.
If an error occurs in IE, the normal code will continue to execute; in FF, the code will end; Safari only supports the onerror event processing of Image.
Image.onerror
onerror can also be applied to other HTMLElements, the most common one is the element
Handling Error:
Determine the error type :
catch(e){
if(e.name=="RangeError")
alert("Error prompt");}
or
catch(e){
if(e instanceof TypeError)
alert("Error prompt");}