Javascript Error object Error handling

Source: Internet
Author: User

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 ");}

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.