Error object
Property:
Name: Error name
Number: error number
Description: description
Message: error message, which is the same as description.
FF Only attribute
FileName: The file where the error occurs.
Stack: Call stack when an error occurs
Constructor:
Error (){
This (0 ,"")}
Error (description ){
This (0, description )}
Error (number, description ){
....}
The constructor parameter does not contain name because the name of the Error object corresponds to its source:
EvalError: the error occurs in eval ().
SyntaxError: syntax error. The error occurs in eval (). Because of other points, SyntaxError cannot be passed through the interpreter.
RangeError: The value is out of the range.
ReferenceError: reference unavailable
TypeError: The variable type is not expected
URIError: the error occurs in encodeURI () or decodeURI ().
Throw an Error:
Throw new Error (0, "Error Demo ");
New Error can be omitted:
Throw ("Error Demo ");
Capture Error:
Try catch finally statement:
Try {
Statements that may be wrong ..}
Catch (e ){
... Processing after an error occurs ..}
Finally {
... The statement block executed after completion ..}
Finally is not required
If nested, do not use the same parameter name for both catch to avoid overwriting.
The input parameter is an Error object from which an Error message is obtained.
FF supports multiple catch entries in one try. It is not recommended because Js is of a weak type.
Window. onerror error Capture:
Window. onerror = function (Msg, Url, Num ){}
The onerror event is passed to three default parameters of the callback function.
Msg: error message
Url: the Url of the file with an error
Num: the row number at which the error occurred.
Window. onerror can also handle SyntaxError, which is more powerful than try catch.
However, onerror belongs to bom, so different browser manufacturers have different support for it.
If an error occurs in IE, the normal code continues to be executed. In FF, the Code ends. Safari only supports onerror event processing in Image.
Image. onerror
Onerror can also be applied to other htmlelements. The most common element is the element.
Handle Error:
Error Type:
Catch (e ){
If (e. name = "RangeError ")
Alert ("error prompt ");}
Or
Catch (e ){
If (e instanceof TypeError)
Alert ("error prompt ");}