JavaScript Error object fault handling _javascript tips

Source: Internet
Author: User
Tags eval try catch
Error Object

Property:
Name: Wrong name
Number: Error
Description: Description
Message: Error messages, multiple description
FF only Property
FileName: The file where the error occurred
Stack: Call stack when an error occurs


Constructor:
Error () {
This (0, "")}

Error (description) {
This (0,description)}

Error (number,description) {
....}

The constructor parameter does not take name because the name of the Error object corresponds to its origin:
Evalerror: Error occurred in Eval ()
SyntaxError: syntax error, error occurred in Eval () because other points occurred syntaxerror could not pass the interpreter
Rangeerror: Value out of range
Referenceerror: Reference not available
TypeError: Variable type not expected
Urierror: Error occurred in encodeURI () or decodeURI ()

To throw an error:
throw new error (0, "Error Demo");
The new error can be omitted:
Throw ("Error Demo");

Catch error:
Try Catch Finally statement:
try{
.. Statements that may be wrong ...}
catch (e) {
.. Processing after an error occurs ...}
finally{
.. Block of statements executed after completion ...}
Finally, it's not necessary.
If nested, two catch do not use the same parameter name to avoid overwriting
The incoming parameter is an Error object from which you can get an error message
FF supports a try multiple catch because JS is not recommended for weak types

Window.onerror Error Trapping:
Window.onerror=function (msg,url,num) {}
The OnError event is passed to the callback function with 3 default parameters
MSG: Error message
URL: The URL of the file where the error occurred
Num: The line number where the error occurred
Window.onerror can also handle syntaxerror, which is more powerful than try catch.
But onerror belong to BOM, so the support of each browser manufacturer is different.
If the error occurred in IE, the normal code will continue to execute; in FF, the code will end; Safari only supports onerror event handling for image.

Image.onerror
OnError can also be applied to other htmlelement, most commonly elements


Handling Error:
To determine the type of error:
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.